# ES6 operators

The operator can be defined as a symbol that tells the system to implement a particular operation. In JavaScript, there is a rich set of operators, and by using specific operators, you can perform any particular task.

The operators are used in the expressions for evaluating different operands.

An expression is a kind of statement that returns a value. The expression includes:

• Operators: It is responsible for performing some operations on operands
• Operands: It represents the data.

For example: Suppose an expression like x*y. In this expression, x and y are the operands, and the asterisk (*) symbol is the multiplication operator.

## Types of Operators

Operators in JavaScript can be classified as:

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

Let us try to elaborate on these operators in detail.

### Arithmetic Operators

Arithmetic operators are the basic mathematical operators that are available in JavaScript ES6. These operators are responsible for performing all mathematical operations such as addition, subtraction, etc. in JavaScript.

Operators Functions
+ (Addition) It returns the sum of the value of operands
- (Subtraction) It returns the difference between the value of operands
* (Multiplication) It returns the product of operands values.
/ (Division) It is used to perform division, and it returns quotient.
% (Modulus) It also performs division and returns the remainder.
++ (Increment) It increments the value of a variable by one.
- (Decrement) It decrements the value of a variable by one.

For example

In this example, we are using all arithmetic operators that are listed above:

Output:

When you execute the above code in the terminal, you will get the following output:

```Addition : 50
Subtraction: 10
Multiplication: 600
The division will give you the quotient: 1.5
Modulus will give you the Remainder: 10
Value of x after pre-increment: 31
Value of x after post-increment: 31
Value of y after pre-decrement: 19
Value of y after post-decrement: 19
```

### Relational Operators

Relational operators are used for comparing the two values and return either true or false based on the expression. These operators are sometimes called Comparison Operators.

Operator Function
> (Greater than) It returns true if the left operand is greater than right operand else it returns false.
< (Less than) It returns true if the left operand is smaller than right operand else it returns false.
>= (Greater than or equal to) It returns true if the left operand is greater than or equal to right operand else it returns false.
<= (Less than or equal to) It returns true if the left operand is smaller than or equal to right operand else it returns false.
== (Equality) It returns true if the value of both operands is the same else it returns false.
!= (Not Equal to) It returns true if the value of operands is not the same else it returns false.

For example:

In this example, we are using all relational operators that are listed above

Output:

When you execute this code in the terminal, you will get the following output:

```Value of x: 20
Value of y: 15
x is greater than y: true
x is smaller than y: false
x is greater than or equal to  y: true
x is smaller than or equal to y: false
x is equal to y: false
x not equal to  y: true
```

### Logical Operators

Logical operators are generally used for combining two or more relational statements. They also return Boolean values.

Operators Description
&& (Logical AND) This operator returns true if all relational statements that are combined with && are true, else it returns false.
|| (Logical OR) This operator returns true if at least one of the relational statements that are combined with || are true, else it returns false.
! (Logical NOT) It returns the inverse of the statement's result.

For example:

In this example, we are using all logical operators that are listed above.

Output:

```Value of x = 30
Value of y = 80
(x < 40) && (y <= 90):  true
(x == 50) || (y > 80):  false
!((x > 20) && (y >= 80)):  false
```

### Assignment Operators

Assignment operators are used for assigning a value to the variable. The operand on the left side of the assignment operator is a variable, and the operand on the right side of the assignment operator is a value.

The right-side value must be of the same data-type of the left-side variable; otherwise, the compiler will raise an error.

Operators Functions
= (Simple Assignment) It simply assigns the value of the right operand to the left operand
+= (Add and Assignment) This operator adds the value of the right operand to the value of the left operand and assigns the result to the left operand.
-= (Subtract and Assignment) This operator subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand.
*= (Multiply and Assignment) This operator multiplies the value of the right operand to the value of the left operand and assigns the result to the left operand.
/= (Divide and Assignment) This operator divides the value of the right operand to the value of the left operand and assigns the result to the left operand.

For example:

In this example, we are using all logical operators that are listed above.

Output:

```After assignment the value of x is:  40
x+=y: 80
x-=y: 40
x*=y: 1600
x/=y: 40
x%=y: 0
```

### Bitwise Operators

Bitwise operators are used for performing the bitwise operations on binary numerals or bit patterns that involve the manipulation of individual bits. Bitwise operators perform the operation on the binary representation of arguments

Generally, bitwise operators are less used and relevant for the applications and hyper-performance programs.

Operator Description
Bitwise AND (&) It compares every bit of the first operand to the corresponding bit of the second operand. If both of the bits are 1, then the result bit will set to 1, else it will set to 0.
Bitwise OR (|) It compares every bit of the first operand to the corresponding bit of the second operand. If both of the bits are 0, then the result bit will set to 0, else it will set to 1.
Bitwise XOR (^) It takes two operands and does XOR on each bit of both operands. It returns 1 if both of the two bits are different and returns 0 in any other case.
Bitwise NOT (~) It flips the bits of its operand, i.e., 0 becomes 1 and 1 becomes 0.
Left shift (<<) It shifts the value of the left operand to the left by the number of bits specified by the right operand.
Sign-propagating Right shift (>>) It shifts the value of the left operand to the right by the number of bits specified by the right operand. This is sign-propagating because the bits that we are adding from the left depends upon the sign of the number (0 represents positive, and 1 represents negative).
Zero-fill right shift It accepts two operands. The first operand specifies the number, and the second operator determines the number of bits to shift. Every bit gets shifted towards the right, and the overflowing bits will be discarded. Because the 0-bit is added from the left, that's why it is a zero-fill right shift.

For example:

In this example, we are using all logical operators that are listed above.

Output:

```Value of 70 in binary 0100 0110
Value of 80 in binary 0101 0000
Value of x & y = 64

Value of x | y = 86

Value of x ^ y = 22

Value of ~ x = -71

Value of x << 2 = 280

Value of x >> 2 = 17
```

### Miscellaneous Operators

These are the operators that perform different operations in different circumstances.

Operators Description
+ (Concatenation Operator) It applies to strings and appends the second string to first.
- (Negation Operator) It changes the sign of the value.
? (Conditional Operator) It is used for representing the conditional expression. It is also called a ternary operator.

Let us try to understand the miscellaneous operators in detail:

### The Negation Operator (-)

It is used to change the sign of the value.

For example:

Output:

```Value of num1 = 80
Value of num2 = -80
```

### The Concatenation Operator (+)

It applies on strings and appends the second string to first. You can understand it by using the following example:

Example:

Output:

```HelloWorld
Welcome Back
```

The concatenation operator does not add the space between the strings. It concatenates multiple strings in a single statement. If you want to show the space between your strings, then you have to define it manually. In the above example, the string "HelloWorld" does not contain any space, but the second string "Welcome Back" has space because we have manually added it.

### The Conditional Operator (?)

This operator represents the conditional expression. It is also called the 'ternary operator.'

Syntax:

Where,

condition: It refers to the conditional expression.

value1: If the condition is true, then this value will be returned.

value2: If the condition is false, then this value will be returned.

Example:

Output:

```Yes 30 is greater than 20
```

### Type Operators

It is a unary operator that returns the data type of the operand.

Syntax:

You can see the data types and values in the following table that are returned by the typeof operator in JavaScript:

Type String Returned by typeof
String "string"
Boolean "boolean"
Number "number"
Object "object"

Example:

Output:

```Variable a is number
Variable b is boolean
Variable c is a string
Variable d is a string
Variable e is undefined
```

In the above example, the variable e is not defined (or not initialized); that's why the typeof operator is giving its type undefined.

If you take the Boolean Values within the quotes, then they will be treated as a string as you can see the variable 'd' in the above example.

Next TopicES6 Loops

### Feedback   