JavaScript Operators

JavaScript Operators are used for performing various mathematical operations.

Types of Operators
  1. Arithmetic Operators 
  2. Comparison Operators
  3. Assignment Operators 
  4. Logical Operators
  5. Bitwise Operators 
  6. Ternary Operator or Conditional Operator
Arithmetic Operators: Used for performing addition, subtraction, multiplication, division, modulus, increment and decrement operations.

Operator Description Example Input Output
+ Addition a = b + c b = 1; c = 2 ; a = 3;
- Subtraction a = b - c b = 3; c = 1; a = 2;
* Multiplication a = b * c b = 2, c =2; a = 4;
/ Division a = b / c b = 4, c = 2; a = 2;
% Modulus a = b % c b = 8, c = 2; a = 2;
++ Increment a = ++b
a = b++
b = 2; a = 3; b = 2;
a = 2; b = 3;
-- Decrement a = --b
a = b--
b = 2; a = 1; b = 1;
a = 2; b = 1;

Comparison Operators: Used for comparing between two operand by value and also by their data types.

Operator Description Examples Inputs passed to a and b Returns below outputs
== Compares the values a == b a = 2; b = 2;
a = 2, b = 1;
true
false
=== compares the values and value type a === b a = 2; b = 2;
a = 2; b = "i";
true
false
!= Not equal to a != b a = 2 ; b = 2;
a = 2 ; b = 1;
false
true
!== Not equal to value and value type a !== b a = 2 ; b = 2;
a = 2 ; b = "i";
false
true
> Greater than a > b a = 2 ; b = 2;
a = 2 ; b = 1;
false
true
>= Greater than and equal to a >= b a = 2 ; b = 2;
a = 2 ; b = 1;
true
true
< Less than a < b a = 2 ; b = 2;
a = 2 ; b = 1;
false
false
<= Less than and equal to a <= b a = 2 ; b = 2;
a = 2 ; b = 1;
true
false

Assignment Operators: Performs the operation before assigning the value to the variables.

Operators Description Example Inputs of a and b Resultant output of a
= Simple assignment a = b b = 1; a = 1;
+= Adds the value and assigns a += b a = 2; b = 3; a = 2 + 3 = 5;
-= Subtracts the value and assigns a -= b a = 2; b = 1; a = 2 - 1 = 1;
*= Multiplies the value and assigns a *= b a = 2; b = 3; a = 2 * 3 = 6;
/= Divides by value and assigns a /= b a = 4; b = 2; a = 4 / 2 = 2;
%= Modulus by value and assigns a %= b a = 9; b = 2; a = 9 % 2 = 1;

Logical Operators: Resultant output will be boolean either one or zero for the logical operators.

Operator Description Example Inputs Outputs
&& Logical and a && b a = 1; b = 1;
a = 2; b = 1;
1
0
|| Logical or a || b a = 0; b = 0;
a = 1; b = 0;
0
1
! Logical not ! ( a == b ) a = 2; b = 2;
a = 2; b = 1;
0
1

Bitwise Operators: Operates on each bit and results the output in decimal.


Operators Description Example Inputs of a and b Resultant output of a
& Bitwise and a & b a = 1101; b = 1100; a = 1100;
| Bitwise or a += b a = 1100; b = 0011; a = 1111;
^ Bitwise xor a ^ b a = 1100; b = 1010; a = 0110;
~ Not, inverts the bit value ~a a = 1000; a = 0111;
<< Shifts the bits left with zero filled from right a << b a = 0010; b = 2; a = 0100; // 4 in decimal
>> Shifts the bits right, discards the shifted bits, signed shift from left a >> b a = 1000; b = 2; a = 0010;
>>> Shifts the bits right, discards the shifted bits, zero filled from left a >>> b a = 1000; b = 2; a = 0010;

Ternary Operator or Conditional Operator: Assignment to the variable is done with respect to the condition. If the condition meets, first value will gets assigned else the second value.
Syntax: variable = condition ? value1 : value2 ;
Example:  a = ( b == c ) ? 2 : 3;
If the value of b is 2 and c is also 2, condition is met, value 2 will get assigned to a
If the value of b is 2 and c is 1, condition is not met, value 3 will get assigned to a