JavaScript Operators

JavaScript Operators

 

Like any other programming languages JavaScript also provides the various types of the Operators. The list of the JavaScript Operators is provided below.

 

  1. Arithmetic Operators
  2. String Concatenation
  3. Increment / Decrements Operators
  4. Comparison Operators
  5. Logical Operators
  6. Assignment Operators
  7. Ternary Operator

 

Arithmetic Operators

Arithmetic Operators are used to perform the mathematical operations on the numbers.  The list of the Arithmetic operators is given below.

Operators Description
+ Addition
Subtraction
* Multiplication
/ Division
% Modulus

We can use the arithmetic operators on the variables. I know you know the each and every operators very personally, so it is easy to you to use this operators.

NOTE: In JavaScript while using the Division operations with Integer or Floats it always returns the Floats value like 10/3 will returns the 3.33 not the 3.

var a = 10;
var b = 3;
var ans = a / b;
alert(ans);

Modulo Operator (%)

Modulo operator always returns the reminder of the two operands.

alert(10 % 3); // returns 1
alert(9 % 3); // returns 0
alert(15 % 4); // returns 3

String Concatenation (+)

JavaScript also allow you to combine the 2 strings. For string concatenation you can use the plus sign (+). If you use the plus sign with the 1 string and 1 integer than JavaScript will combine and returns the string.

var a = "Hello ";
var b = "world ";
var c = 3;

alert(a + b); // returns "Hello world"
alert(a + c); // returns "Hello 3";

Increment / Decrements Operator (++ / –)

This operators is useful when you want to increment or decrements the variable with the 1. This operators uses by default 1 for the increment and decrements in variable. Variable must be type of the integer.

For increment we can use the (++) sign with variables and for the decrements we can use the (–) with the variables.

Like any other programming languages JavaScript also provide the Post Increments / Decrements and the Pre Increments / Decrements.

Post increments / decrements means the value of variable is first used with another operation after that increments / decrements of the variable is done with 1. For post increments / decrements we can use the (++ / –) sign after the name of variable like a++; & a–;

var a = 1;
a++; // now the value of a will be 2.
alert(a);

a--; // now the value of a will be 1.
alert(a);

Pre increments / decrements means the value of the variable is increment or decrements by 1 and after that it will be used by another operation. For pre increment / decrements we can use the (++ / –) sign before the name of variable like –a; & ++a;

var a = 1;
++a; // now the value of a will be 2.
alert(a);

--a; // now the value of a will be 1.
alert(a);

Comparison Operators

JavaScript also provides the comparison operators. This Operators is used when we want to compare the value of two operands. It always returns the Boolean value either true or false.

Operators Description
== It checks the equality of the two operands with its value.
=== It checks the equality of the two operands with its type and value.
!= It checks the inequality of two operands
> It check the left side value is greater than or not. If left side value is greater than right side value than it returns the true, otherwise it returns the false.
< It check the right side value is greater than or not. If right side value is greater than left side value than it returns the true, otherwise it returns the false.
>= It check the left side value is greater or equal to. If left side value is greater than or equal to right side value than it returns the true, otherwise it returns the false.
<= It check the right side value is greater or equal to. If right side value is greater than or equal to left side value than it returns the true, otherwise it returns the false.
var a = 1;
var b = "1";
var c = 2;

alert(a == b); // returns the true
alert(a === b); // returns the false
alert(a != c); // returns the true
alert(a > c); //returns the false
alert(a >= c); //returns the false
alert(a <= c); // returns the true

Logical Operators

JavaScript also provides the logical operators it useful to checks the two or more conditions.

Operators Descriptions
&& && operator is also known as the And operator. It is useful when you have to checks the two or more conditions is true or not. If all the conditions checked by the && is true than it returns the true, If one of conditions of them is wrong than it returns false.
!! || operator is also known as the Or operator. It is useful when you have to checks the one or more conditions is true or not. If all the conditions checked by the !! and one of the conditions is true than it returns the true otherwise it returns the false.
! ! operator is also known as the Not operator. It simply reverse the boolean result of the condition.
var a = 1;
var b = "1";
var c = 2;

if (!(a > c)) {
    alert("Value of a is less than b."); // this popup will be displayed
}

if (!(a == b)) {
    alert("value of a and b is equal."); // this popup will be not displayed
}

Assignment Operators

JavaScript also provides the assignment operators. It is useful to do action on the left and right side operands and assign the result to the left side operands.

Operators Description
= It simply assign the right side value to the left side operands
+= It will perform the addition on the left side and right side value and assign the result to the left side operands.
-= It will perform the subtraction on the left side and right side value and assign the result to the left side operands.
*= It will perform the multiplication on the left side and right side value and assign the result to the left side operands.
/= It will perform the division on the left side and right side value and assign the result to the left side operands.
%= It will perform the divide the left side value with right side value and assign the reminder(modulo) to the left side operands
var a = 10, b = 5;

a += b; // the value of a becomes the 15.

a -= b; // the value of a becomes the 10.

a *= b; // the value of a becomes the 50.

a /= b; // the value of a becomes the 10.

a %= b; // the value of a becomes the 0

Ternary Operator

JavaScript also provides the ternary operator which is helpful to assign the value to the variable based on the some condition. This operator is combination of the ? & .

Ternary operator starts with the condition and have the true part and false part. It is also known as the short form of the if..else condition.

Syntax of the ternary operator


var a = "";

a = <condition> ? true part here : false part here;

Example of the ternary operator

var a = 5, b = 10;
var c = "";

c = a == b ? "true" : "false"; // it will assign the false to the c.

alert(c);

Happy Coding. 🙂