Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6

The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators. By using a specific operator, we can perform a specific task. In expressions, the operators are used to classify the different operands.

An expression can be defined as a type of statement that returns a value. An expression contains two following aspects-

Operator: An operator is used to determine the operations performed on the operands. 

Operand: The operands are used to represent the data.

For Example- A + B

                         A - B

                         A * B

                         A / B

In the example given above, A and B are the operands and (+, -, *, /) are the operator symbols used to perform the operations on the operands.

Types of Operators    

 ECMAScript 6 programing language supports the following operators.

Now, we will discuss each operator in detail.

Arithmetic Operator

Arithmetic operators are used to perform basic mathematic operations such as subtraction, addition, multiplication, and division.

Operators                       Function
Addition (+) It is used to return the sum of operands.
Subtraction (-) It is used to return the difference between the operands.
Multiplication (*) It is used to return the product of operands.
Division (/) It is used to return the quotient and perform a division operation on operands.
Increment (+ +) It is used to perform increments of the operand by one (+1).
Decrement (- -) It is used to perform decrement of the operand by one (-1)
Modulus (%) It is also used to perform a division operation and returns a reminder.

For Example

We have taken an example to use the arithmetic operator.

var a = 40;
 var b = 20;
 console.log (“Addition: ” + (a + b));
 console.log (“Subtraction: ” + (a - b));
 console.log (“Multiplication: ” + (a * b));
 console.log (“The division returns the quotient: ” + (a / b));
 console.log (“The value of a after pre-increment: ” + (++ a));        // pre- increment 
 console.log (“The value of a after post-increment: ” + (a ++));      // post- increment
 console.log (“The value of b after pre-decrement: ” + (-- b));      // pre- decrement
 console.log (“The value of b after post-decrement: ” + (b --));      // post- decrement
 console.log (“The modulus returns the reminder: ” +(a % b)); 

Output

After the execution of the above-given code, we will get the following output.  

Operators in ECMAScript 6

Logical Operators

Logical Operators are used to make decisions based on various conditions. The logical operators help us to associate two or more than two conditions. These operators return the Boolean value.

Some logical operators used in ECMAScript 2015 are listed below-

                    Operator                      Function
Logical AND (&&) It is used to a return true value if the relational statement is associated with Logical AND (&&) otherwise, it returns false.
Logical OR (| |) It is used to return true if at least one statement is associated with Logical Or (| |) otherwise false.
Logical NOT (!) It is used to return the inverse value of the expression.

For Example

We have taken an example to use the Logical operator.

var r = 5;
 var s = 20;
 console.log (“value of r = ” + r);
 console.log (“value of s = ” + s);
 var res = ((r < 8) && (s <= 30));
 console.log (“(r < 8) && (s <= 30): ”, res); 
 var res = ((r == 8) || (s > 25));
 console.log (“(r == 8) || (s > 25): ”, res);
 var res = !((r > 3) && (s >= 20));
 console.log (“!(r > 3) && (s >= 20): ”, res); 

Output

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

Assignment Operator

The Assignment Operators provide us the facility to assign a value to the variable.  The assignment operator assigns a value to its left operand. The value of the right operand is based on the left operand. Both (Right and Left) value must have the same data type; otherwise, it will cause an error.

Some Assignment operators and functions are given below in the table-

Operator                  Function
Add and Assignment (+ =) It is used to add the value of the right operand to the left operand and allot the result to the left operand.
Subtract and Assignment (- =) It is used to find the difference between both operands and store the result to the left operand.
Multiply and Assignment (* =) This operator is used to multiply the value of the right operand to the value of the left operand and store the result in the left operand.
Divide and Assignment (/ =) By using this operator, we can divide the value of the right operand to the value of the left operand and store the result in the left operand.
Simple and Assignment (=) This operator is used to allot the value from the right operand to the left operand.

For Example

We have taken an example to use the Assignment operator.

var u = 12;
 var v = 10;
 u += v;                                                                 // Add and assignment
 console.log (“u += v:” + u);
 u -= v;                                                                  // Subtract and assignment
 console.log (“u -= v:” + u);
 u *= v;                                                                  // Multiply and assignment 
 console.log (“u *= v:” + u);
 u /= v;                                                                  // Division and assignment
 console.log (“u /= v:” + u);
 u = v;                                                                  // Simple and assignment
 console.log (“The value of u after the assignment is:” + u); 

Output

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

Relational Operator

In a programming language, the relational operators are used to find the relation between two operands. After comparing the two operands, we will get the result in the form of true or false based on the expression. The relational operators are also known as Comparison Operator.

Some Relational operators and their functions are listed below-

Operator                    Function
Greater than (>) It returns the true when the value of the left operand is greater than the value of the right operand; otherwise the result will be false.
Less than (<) It returns true when the left operand is less than the right operand; otherwise the result will be false.
Less than equal to (< =)                                             It returns the true when the value of the left operand is less than or equal to the value of the right operand; otherwise, the result will be false.
Greater than equal to (> =) It returns the true when the value of the left operand is greater than or equal to the value of the right operand; otherwise, the result will be false.
Equality (= =) This operator returns true when the value of both operands (right and left) are equal; otherwise, the result will be false.
Not equal (! =) It returns true when the value of both operands (right and left) are not the same; otherwise, the result will be false.

For Example

We have taken an example to use the Relational operator.

var a = 30;
 var b = 25;
 console.log (“value of a: ” a);
 console.log (“value of b: ” b);
 var res = a > b;
 console.log (“a is greater than b: ” + res);
 res = a < b;  
 console.log (“a is smaller than b: ” + res);
 res = a <= b; 
 console.log (“a is smaller than or equal to b: ” + res);
 res = a >= b; 
 console.log (“a is greater than or equal to b: ” + res);
 res = a == b; 
 console.log (“a is equal to b: ” + res);
 res = a != b;  
 console.log (“a is not equal to b: ” + res); 

Output

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

Bitwise Operator

The Bitwise operators help us to perform the bitwise operations on the bit pattern or binary numerals. The operations involve the manipulation of bits (individual). The bitwise operations are simple and fast, supported by the processor. It is used to manipulate the values for calculation and comparison.

Some Bitwise operations and their functions are listed below-

Operator                      Function
Bitwise AND (&)   This operator compares each bit of the first operand to the bit of the second operand. If both bits are 1, then the result bit will be 1 otherwise 0.  
Bitwise OR (|)   This operator compares each bit of the first operand to the bit of the second operand. If both bits are 0, then the result bit will be 0 otherwise 1.
Bitwise NOT (~)   It is used to invert the bits of operands such as 0 converts into 1 and 1 converts into 0.
Bitwise XOR (^)   This operator performs XOR operation on each bit of both operands. If a bit of each operand is different, then the result will be 1 otherwise 0.  
Left shift (<<)   This operator is used to shift the bit of the left operand to the left side by the bits of the right operand.
Sign-propagating right shift (>>)   It helps us to shift the bits of the left operand to the left side by the bits of the right operand. If the added number of bits is 0, then positive otherwise negative.
Zero-fill right shift (>>>)   It is used to shift every bit to the right side.  Extra bits will be discarded. It is called Zero-fill cause of the added bits from the left are 0.

For Example

We have taken an example to use the Bitwise operator.

var a = 70;
 var b = 80;
 var result = 0;
 console.log (“Value of 70 in binary 0100 0110”);
 console.log (“Value of 80 in binary 0101 0000”);
 result = a & b;
 console.log (“value of a & b =%d\n”, result); 
 result = a | b;
 console.log (“value of a | b =%d\n”, result);
 result = a ~ b;
 console.log (“value of a ~ b =%d\n”, result);
 result = a ^ b;
 console.log (“value of a ^ b =%d\n”, result);
 result = a << b;
 console.log (“value of a << b =%d\n”, result); 
 result = a >> b;
 console.log (“value of a >> b =%d\n”, result); 

Output

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

Miscellaneous Operator

The Miscellaneous operators are used to perform various operations in various conditions. It includes 3 types of following operators listed below-

Operator                   Function
The Negative Operator (-) A Negative operator modifies the symbol of a value.
The Concatenation Operator (+) It is mostly implemented on strings. It connects the second string to first.
The Conditional Operator (?) It is also known as Ternary Operator. This operator is used to present the conditional expressions.

We have discussed each operator in detail with an example-

The Negative Operator (-)

We can change the symbol of decision value by using the negative operator.

For Example

We have taken an example to use the negative operator.

var p = 20;
 var q = -p;
 console.log (“value of p = ” +p);
 console.log (“value of q = ” +q); 

Output                                                                                                     

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

The Concatenation Operator (+)

We can link or connect two strings by using the concatenation operator. This operator is usually implemented on strings.

For Example

We have taken an example to use the Concatenation operator.

var r = ‘Hello’ + ‘ECMAScript 6’;
 var s = ‘Hello’ + ‘JavaScript’;
 console.log (r);
 console.log (s); 

Output

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

The Conditional Operator (?)

This operator is used to present the conditional expressions. The conditional operator is also known as Ternary Operator.

For Example

We have taken an example to use the Conditional operator.

var n1 = 40;
 var n2 = 30;
 var result = n1 > n2 ? “40 is greater than 30”;
 Console.log (result); 

Output

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

Typeof Operator

The typeof operator is used to return the data type of an operand. It is also known as Unary Operator.

Syntax of Typeof Operator

Typeof (operand)                          // syntax of typeof operator

Here, some values and their data types returned by the type operator is listed below-

                     Type  Returned String by typeof operator
                    Object                     “object”
                    Boolean                     “boolean”
                     String                     “string”
                    Number                     “number”

For Example

We have taken an example to use the Typeof operator.

var p = 200;
 var q = ‘hello ES6’;
 var r = ‘false’;
 var = ‘true’;
 var t;
 console.log (“variable p is a ” +typeof (p));
 console.log (“variable q is a ” +typeof (q));
 console.log (“variable r is a ” +typeof (r)); 
 console.log (variable s is “ +typeof (s)”);
 console.log (variable t is “ +typeof (t)”); 

Output

After the execution of the above-given code, we will get the following output. 

Operators in ECMAScript 6

In the output, if we define any value in quotes, then it will consider as a string value. The variable t is undefined here.