Javascript Operators

Let us consider a simple expression. 7 + 5 equals 12  . The operands, in this case, are 7 and 5, and the operator, in this case, is '+.' The following operators are supported by JavaScript.

  1. Comparison Operators
  2. Arithmetic Operators
  3. Assignment Operators
  4. Ternary (Conditional) Operators
  5. Relational (Logical) Operators
  6. Bitwise Operators

Now, we'll be taking a look at each of these Javascript operators in detail. We'll be highlighting the operator, giving descriptions of each operator and examples of the operators.

1. Comparison Operators

For the examples under Comparison operators, assume variable X holds Five and variable Y has 10:

OperatorDescription of OperatorExamples
==equal to(X==Y) is false
===equal value(X===Y) is false
>greater than(X>Y) is false
<less than(X<Y) is true
!=not equal(X!=Y) is true
!==not equal value(X!==Y) is true
>=Greater than or equal to(X>=Y) is false
<=less than or equal to(X<=Y) is true
?Ternary Operator

2. Arithmetic Operators

For the examples under Arithmetic operators, assume variable X holds Five and variable Y has 10:

OperatorDescription of OperatorExamples
+AdditionX+Y = 15
-SubtractionY-X = 5
/DivisionY/X = 2
*MultiplicationX*Y = 50
++IncrementX++ = 6
--DecrementX-- = 4
%Modulus or RemainderY%X = 0

3. Assignment Operators

=Assigns the right operand's value to the left operand.
+=Sums up the left and right operand values and then assigns the result to the left operand.
-=Subtracts the right operand's value from the left operand's value and then assigns the result to the left operand.
*=Multiply both values and then assigns the result to the left operand.
/=Divide the left by the right operand value, and then assigns the result to the left operand.
%=Get the modulus of the left operand, divide it by the right operand, and assign the resulting modulus to the left operand.

4. Ternary (Conditional) Operators

The conditional operator examines an expression to determine if it is true or false and then runs one of the given two statements based on the outcome of the evaluation.

Run this code to see how Conditional Operators work in JS:

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var x = 20;
            var y = 30;
            var linebreak = "<br />";
         
            document.write ("((x > y) ? 200 : 300) => ");
            result = (x > y) ? 200 : 300;
            document.write(result);
            document.write(linebreak);
         
            document.write ("((x < y) ? 200 : 300) => ");
            result = (x < y) ? 200 : 300;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>      
      <p>Run this code by TechStack...</p>
   </body>
</html>

5. Relational (Logical) Operators

For the examples under Relational (Logical) operators, assume variable X holds Five and variable Y has 10:

OperatorDescription of OperatorExamples
||Logical OR(X || Y) is true
&&Logical AND(X && Y) is true
!Logical NOT! (X && Y) is false

6. Bitwise Operators

Bit operators operate on numbers in 32 bits.

The operation converts any numeric operand into a 32-bit integer. The output is then returned as a JS number.

OperatorDescription of OperatorExamples
&Bitwise AND10 & 5 same as 1010 & 0101
|Bitwise OR10 | 5 same as 1010 | 0101
^Bitwise XOR10 ^ 5 same as 1010 ^ 0101
<<Left Shift10 << 5 same as 1010 << 0101
>>Right Shift10 >> 5 same as 1010 >> 0101
>>>Right Shift + Zero10 >>> 5 same as 1010 >>> 0101
~Bitwise NOT~ 10 same as ~ 1010