ES6 | Operators

07 Mar, 2022
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. 
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. 
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. 
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. 
 
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. 
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. 
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
 
sumitgumber28
ES6
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.