Types of Operators in JavaScript’s –This chapter describes JavaScript’s expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, ternary and more.

Table of Contents

## Types of Operators in JavaScript’s

- Assignment operators
- Comparison operators
- Arithmetic operators
- Bitwise operators
- Logical operators
- String operators
- Conditional (ternary) operator
- Comma operator
- Unary operators
- Relational operators

JavaScript has both *binary* and *unary* operators, and one special ternary operator, the conditional operator. A binary operator requires two operands, one before the operator and one after the operator:

```
4 + 2
X * Y
```

A unary operator requires a single operand, either before or after the operator:

```
X++
++X
```

## JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

Operator | Example | Meaning |
---|---|---|

Assignment `(=)` | `x = y` | `x = y` |

Addition assignment `(+=)` | `x += y` | `x = x + y` |

Subtraction assignment `(-=)` | `x -= y` | `x = x - y` |

Multiplication assignment `(*=)` | `x *= y` | `x = x * y` |

Division assignment `(/=)` | `x /= y` | `x = x / y` |

Remainder assignment `(%=)` | `x %= y` | `x = x % y` |

Left shift assignment `(<<=)` | `x <<= y` | `x = x << y` |

Right shift assignment `(>>=)` | `x >>= y` | `x = x >> y` |

Unsigned right shift assignment `(>>>=)` | `x >>>= y` | `x = x >>> y` |

Bitwise AND assignment `(&=)` | `x &= y` | `x = x & y` |

Bitwise XOR assignment `(^=)` | `x ^= y` | `x = x ^ y` |

Bitwise OR assignment `(|=)` | `x |= y` | `x = x | y` |

Exponentiation assignment `(**=)` | `x **= y` | `x = x ** y` |

Logical AND assignment `(&&=)` | `x &&= y` | `x && (x = y)` |

Logical OR assignment `(||=)` | `x ||= y` | `x || (x = y)` |

Logical nullish assignment `(??=)` | `x ??= y` | `x ?? (x = y)` |

## JavaScript Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

Operator | Description | Examples returning true |
---|---|---|

Equal (`==` ) | Returns `true` if the operands are equal. | `x == 3` |

Not equal (`!=` ) | Returns `true` if the operands are not equal. | `x != 4` |

Strict equal (`===` ) | Returns `true` if the operands are equal and of the same type. | `x === 3` |

Strict not equal (`!==` ) | Returns `true` if the operands are of the same type but not equal, or are of different type. | `x !== "lion"` |

Greater than (`>` ) | Returns `true` if the left operand is greater than the right operand. | `x > y` |

Greater than or equal (`>=` ) | Returns `true` if the left operand is greater than or equal to the right operand. | `x >= y` |

Less than (`<` ) | Returns `true` if the left operand is less than the right operand. | `x < y ` |

Less than or equal (`<=` ) | Returns `true` if the left operand is less than or equal to the right operand. | `x <= y` |

## JavaScript Arithmetic Operators

Arithmetic operators perform arithmetic on numbers (literals or variables).

Operator | Description |
---|---|

`+` | Addition |

`-` | Subtraction |

`*` | Multiplication |

`**` | Exponentiation (ES2016) |

`/` | Division |

`%` | Modulus (Remainder) |

`++` | Increment |

`--` | Decrement |

```
let x = 100 + 50;
let x = a + b;
```

## JavaScript Bitwise Operators

Bit operators work on 32 bits numbers.

Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.

The following table summarizes JavaScript’s bitwise operators.

Operator | Usage | Description |
---|---|---|

Bitwise AND | `a & b` | Returns a one in each bit position for which the corresponding bits of both operands are ones. |

Bitwise OR | `a | b` | Returns a zero in each bit position for which the corresponding bits of both operands are zeros. |

Bitwise XOR | `a ^ b` | Returns a zero in each bit position for which the corresponding bits are the same. [Returns a one in each bit position for which the corresponding bits are different.] |

Bitwise NOT | `~ a` | Inverts the bits of its operand. |

Left shift | `a << b` | Shifts `a` in binary representation `b` bits to the left, shifting in zeros from the right. |

Sign-propagating right shift | `a >> b` | Shifts `a` in binary representation `b` bits to the right, discarding bits shifted off. |

Zero-fill right shift | `a >>> b` | Shifts `a` in binary representation `b` bits to the right, discarding bits shifted off, and shifting in zeros from the left. |

Operator | Description | Example | Same as | Result | Decimal |
---|---|---|---|---|---|

& | AND | 5 & 1 | 0101 & 0001 | 0001 | 1 |

| | OR | 5 | 1 | 0101 | 0001 | 0101 | 5 |

~ | NOT | ~ 5 | ~0101 | 1010 | 10 |

^ | XOR | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |

<< | left shift | 5 << 1 | 0101 << 1 | 1010 | 10 |

>> | right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |

>>> | unsigned right shift | 5 >>> 1 | 0101 >>> 1 | 0010 | 2 |

## JavaScript Logical Operators

Logical operators are used to determine the logic between variables or values.

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the `&&`

and `||`

operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

Operator | Description | Example |

&& | AND | `( 5 > 1 && 1 < 5 )` True |

|| | OR | `( 10==1 || 5 == 10 )` False |

! | NOT | `!( 5 == 10 )` True |

## JavaScript String Operators

In addition to the comparison operators, which can be used on string values, the **concatenation** **operator **`(+)`

concatenates two string values together, returning another string that is the union of the two operand strings.

```
console.log('my ' + 'string'); // console logs the string "my string".
```

The shorthand **assignment** **operator** `+=`

can also be used to concatenate strings.

```
var mystring = 'alpha';
mystring += 'bet'; // evaluates to "alphabet" and assigns this value to mystring.
```

## JavaScript Conditional (ternary) Operator

The **conditional** **operator** is the only JavaScript operator that takes three operands. The operator can have one of two values based on a condition.

### Syntax :

```
variablename = (condition) ? value1:value2
```

### Example :

```
let voteable = (age < 18) ? "Too young" : "Old enough";
```

If the variable age is a value below 18, the value of the variable voteable will be “Too young”, otherwise the value of voteable will be “Old enough”.

## JavaScript Unary operators

Unary operators work on one value.

Unary Operators | Name | Meaning |
---|---|---|

+x | Unary Plus | Convert a value into a number |

-x | Unary Minus | Convert a value into a number and negate it |

++x | Increment Operator (Prefix) | Add one to the value |

–x | Decrement Operator (Prefix) | Subtract one from the value |

x++ | Increment Operator (Postfix) | Add one to the value |

x– | Decrement Operator (Postfix) | Subtract one from the value |

I hope you like this Types of Operators in JavaScript’s article.

Also Read :