Open In App

ES6 Operators

Improve
Improve
Like Article
Like
Save
Share
Report

An expression is a special kind of statement that evaluates to a value. Every expression consists of 
 

  • Operands: Represents the data.
  • Operator: which performs certain operations on operands.


Consider the following expression – 2 / 3, in the expression, 2 and 3 are operands and the symbol /is the operator.
JavaScript supports the following types of operators: 
 

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Type Operators
  • Miscellaneous Operators


Arithmetic Operators: As we know these are the basic mathematical operators available in JavaScript ES6.
 

OperatorFunction
Addition(+)Returns sum of the operands.
Subtraction(-)Returns the difference of the values.
Multiplication(*)Returns the product of the values.
Division(/)Performs division and returns the quotient.
Modulus(%)Performs division and returns the remainder.
Increment(++)Increases the value of the variable by one.
Decrement(- -)Decreases the value of the variable by one.


Example: 
 

javascript

<script>
    var num1 = 10.5
    var num2 = 2.5
 
    document.write("Sum : " + (num1 + num2) + "<br>");
 
    document.write("Difference : " + (num1 - num2) + "<br>")
 
    document.write("Product : " + num1 * num2 + "<br>")
 
    document.write("Quotient : " + num1 / num2 + "<br>")
 
    document.write("Remainder : " + num1 % num2 + "<br>")
         
    // pre-increment
    document.write("Value of num1 after pre-increment : "
                + (++num1) + "<br>")
         
    // post-increment
    document.write("Value of num1 after post-increment : "
                + (num1++) + "<br>")
         
    // pre-decrement
    document.write("Value of num2 after pre-decrement : "
                + (--num2) + "<br>")
         
    // post-decrement
    document.write("Value of num2 after post-decrement : "
                + (num2--) + "<br>")
</script>

                    

Output: 
 

Sum : 13
Difference : 8
Product : 26.25
Quotient : 4.2
Remainder : 0.5
Value of num1 after pre-increment : 11.5
Value of num1 after post-increment : 11.5
Value of num2 after pre-decrement : 1.5
Value of num2 after post-decrement : 1.5


Relational Operators: An operator that compares two values. Relational operators are sometimes called comparison operators.
 

OperatorFunction
>Returns true if the left operand is greater than right else, false.
<Returns true if the left operand is lesser than right else, false.
>=Returns true if the left operand is greater than or equal to right else, false.
<=Returns true if the left operand is lesser than or equal to right else, false.
==Returns true if both the operands are same else, false./td>
==Returns true if both the operands are the same else, false.
!=Returns true if both the operands are not same else, false.


Example: 
 

javascript

<script>
    document.write("11>12 : " + (11 > 12) + "<br>");
 
    document.write("11<12 : " + (11 < 12) + "<br>")
 
    document.write("12>=11 : " + (12 >= 11) + "<br>")
 
    document.write("12<=12 : " + (12 <= 12) + "<br>")
 
    document.write("11==11 : " + (11 == 11) + "<br>")
 
    document.write("11!=11 : " + (11 != 11) + "<br>")
</script>

                    

Output: 
 

11>12 : false
11=11 : true
12<=12 : true
11==11 : true
11!=11 : false


Logical Operators: Logical operators are used to combine two or more relational statements.
 

OperatorFunction
And(&&)Return true if all the relational statements combined with && are true, else false.
Or(||)Return true if at least one of the relational statements combined with || is true, else false.<
Not(!)Returns the inverse of the relational statement’s result.


Example: 
 

javascript

<script>
     
    // Returns true as every statement is true.
    document.write("13>12 && 12>11 && 9==9 : "
        + (13 > 12 && 12 > 11 && 9 == 9) + "<br>");
     
    // Returns false as 11>12 is false.
    document.write("11>12 && 12>11 && 9==9 : "
        + (11 > 12 && 12 > 11 && 9 == 9) + "<br>")
         
    // As one true statement is enough to return.
    document.write("11>12 || 12>11 || 9==9 : "
        + (11 > 12 || 12 > 11 || 9 == 9) + "<br>")
         
    // Returns false as 11>12 is not true.
        document.write("11>12 && (12>11 || 9==9) : "
        + (11 > 12 && (12 > 11 || 9 == 9)) + "<br>")
</script>            

                    

Output: 
 

13>12 && 12>11 && 9==9 : true
11>12 && 12>11 && 9==9 : false
11>12 || 12>11 || 9==9 : true
11>12 && (12>11 || 9==9) : false


Bitwise Operators: A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits. 
 

OperatorFunction
Bitwise AND(&)Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 0, the corresponding result bit is set to 0, else 1.
Bitwise OR(|)Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1, else 0.
Bitwise XOR(^)Inverts the bits of its corresponding operand
Left shift(<<)Will shift the ‘n’ number of bits to the left side, and n bits with the value 0 will be filled on the right side. Example: x=2, t=4, x<<t, for easy evaluation it performs x*(2^n)=2*(2^4)  .
Right shift(>>)Will shift the ‘n’ number of bits to the right side, and ‘n’ bits with the value 0 will be filled on the left side. Example: x=2, t=4, x>>t, for easy evaluation it performs t/(2^n)=2/(2^4)  .
Zero-fill right shiftShifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeroes from the left


Example: 
 

javascript

<script>
    document.write("2&3 : " + (2 & 3) + "<br>");
    document.write("2|3 : " + (2 | 3) + "<br>");
    document.write("2^3 : " + (2 ^ 3) + "<br>");
    document.write("~4 : " + (~4) + "<br>");
    document.write("2<<3 : " + (2 << 3) + "<br>");
    document.write("2>>3 : " + (2 >> 3) + "<br>");
</script>

                    

Output: 
 

2&3 : 2
2|3 : 3
2^3 : 1
~4 : -5
2<>3 : 0


Assignment Operators: An assignment operator is the operator used to assign a new value to the variable, property, event or indexer element.
 

OperatorFunction
Simple Assignment(=)Assigns the value of the right operand to left operand.
Add and Assignment(+=)It adds the right operand to the left operand and assigns the result to the left operand.
Subtract and Assignment(-=)It subtracts the right operand from the left operand and assigns the result to the left operand.
Multiply and Assignment(*=)It multiplies the right operand with the left operand and assigns the result to the left operand.
Divide and Assignment(/=)It divides the left operand with the right operand and assigns the result to the left operand.


Example: 
 

javascript

<script>
    var a = 12;
    var b = 10;
    a = b;
    document.write("a = b : " + a + "<br>");
     
    a += b;
    document.write("a += b : " + a + "<br>")
     
    a -= b;
    document.write("a -= b : " + a + "<br>")
     
    a *= b;
    document.write("a *= b : " + a + "<br>")
     
    a /= b;
    document.write("a /= b : " + a + "<br>")
</script>

                    

Output: 
 

a = b : 10
a += b : 20
a -= b : 10
a *= b : 100
a /= b : 10


Type Operators: It is a unary operator. This operator returns the data type of the operand. 
Syntax: 
 

typeof(operand)


Example: 
 

javascript

<script>
    var a = 12;
    var b = "Geeks";
    var b = "Geeks";
    var c = true;
    var d = String;
 
    document.write("a is " + typeof(a) + "<br>");
 
    document.write("b is " + typeof(b) + "<br>")
    document.write("c is : " + typeof(c) + "<br>")
    document.write("d is : " + typeof(d) + "<br>")
</script>

                    

Output: 
 

a is number
b is string
c is : boolean
d is : function


Miscellaneous Operators:These are the operators that, does different operators when used at different types of occasions.
 

OperatorFunction
Negation operator (-)Changes the sign of a value.
Concatenation operator (+)+, when applied to strings it appends them.
Conditional Operator (?)It can use as a ternary operator.


Example: 
 

javascript

<script>
    var a = "GeeksforGeeks=>";
    var b = "A Computer science portal.";
    var c = true;
    var d = 9;
 
    // Concatenation
    document.write("a+b : " + a + b + "<br>");
 
    // Ternary Operator
    var c = 2 > 3 ? "yes is 2>3" : "No, It is not."
    document.write("2>3 :" + c + "<br>")
 
    // Negation
    d = -d;
    document.write("d = -d : " + d + "<br>")
</script>

                    

Output: 
 

a+b : GeeksforGeeks=>A Computer science portal.
2>3 :No, It is not.
d = -d : -9


 



Last Updated : 06 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads