Select Page

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.

Output

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

### 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.

Output

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

### 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.

Output

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

### 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.

Output

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

### 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.

Output

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

### 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.

Output

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

### 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.

Output

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

### 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.

Output

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

### 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

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.

Output

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

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

Share This