In this article, we will learn about Operators in JavaScript.

Contents

## What is an Operator?

An operator is a special symbol in JavaScript that is used to perform operations on operands (values and variables). For example:

4 + 5; // 9 |

Here, operator + performs addition, and 4 and 5 are operands.

## JavaScript Operator Types

The following is a list of the various operators that we will learn in this tutorial.

- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
- Other Operators

### Assignment Operators in JavaScript

Variables are assigned values using the assignment operators. For example:

const a = 7; |

The = operator is used to assign the value 5 to variable x in this case.

Here’s a list of the most common assignment operators:

Operator | Name | Example |
---|---|---|

= | Assignment operator | x = 3; |

+= | Addition assignment | x += 3; // x = x + 3 |

-= | Subtraction Assignment | x -= 3; // x = x – 3 |

*= | Multiplication Assignment | x *= 3; // x = x * 3 |

/= | Division Assignment | x /= 3; // x = x / 3 |

%= | Remainder Assignment | x %= 3; // x = x % 3 |

**= | Exponentiation Assignment | x **= 3; // x = x ** 3 |

**Note**: The assignment operator = is commonly used. Once you’ve learned arithmetic operators, you’ll be able to understand other assignment operators like +=, -=, *=, and so on.

### Arithmetic Operators in JavaScript

To perform arithmetic calculations, arithmetic operators are used. For example:

const number = 7 + 5; // 12 |

The + operator is used to combine two operands in this case.

Here’s a list of the most common arithmetic operators:

Operator | Name | Example |
---|---|---|

+ | Addition | a + b |

– | Subtraction | a – b |

* | Multiplication | a * b |

/ | Division | a / b |

% | Remainder | a % b |

++ | Increment (increments by 1) | ++a or a++ |

— | Decrement (decrements by 1) | –a or a– |

** | Exponentiation (Power) | a ** b |

**Example of Arithmetic Operators:**

let x = 8; let y = 4; // addition console.log('x + y = ', x + y); // 12 // subtraction console.log('x - y = ', x - y); // 4 // multiplication console.log('x * y = ', x * y); // 32 // division console.log('x / y = ', x / y); // 2 // remainder console.log('x % y = ', x % y); // 2 // increment console.log('++x = ', ++x); // x is now 9 console.log('x++ = ', x++); // prints 9 and then increased to 10 console.log('x = ', x); // 10 // decrement console.log('--x = ', --x); // x is now 9 console.log('x-- = ', x--); // prints 9 and then decreased to 8 console.log('x = ', x); // 8 //exponentiation console.log('x ** y =', x ** y);

**Note**: the ** operator was introduced in ECMAScript 2016 and may not be supported by all browsers.

### Comparison Operators in JavaScript

Comparison operators compare two values and return either true or false as a boolean value. For example:

const x = 5, y = 4; console.log(a > b); // true |

The comparison operator > is used to see if x is greater than y in this case.

Here’s a list of the most common comparison operators:

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

== | Equal to: returns true if the operands are equal | a == b |

!= | Not equal to: returns true if the operands are not equal | a != b |

=== | Strict equal to: true if the operands are equal and of the same type | a === b |

!== | Strict not equal to: true if the operands are equal but of different types or not equal at all | a !== b |

> | Greater than: true if the left operand is greater than the right operand | a > b |

>= | Greater than or equal to: true if the left operand is greater than or equal to the right operand | a >= b |

< | Less than: true if the left operand is less than the right operand | a < b |

<= | Less than or equal to: true if the left operand is less than or equal to the right operand | a <= b |

**Example of Comparison Operators:**

// equal operator console.log(7 == 7); // true console.log(7 == '7'); // true // not equal operator console.log(7 != 5); // true console.log('hello' != 'Hello'); // true // strict equal operator console.log(7 === 7); // true console.log(7 === '7'); // false // strict not equal operator console.log(7 !== '7'); // true console.log(7 !== 7); // false

### Logical Operators in JavaScript

Logical operators carry out logical operations and return a true or false boolean value. For example:

const x = 5, y = 4; (x < 9) && (y < 6); // true |

The logical operator AND(&&) is used here. The result is true because both x < 9 and y < 6 are true.

Here’s a list of the most common logical operators:

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

&& | Logical AND: true if both the operands are true, else returns false | a && b |

|| | Logical OR: true if either of the operands is true; returns false if both are false | a || b |

! | Logical NOT: true if the operand is false and vice-versa. | !x |

**Example of Logical Operators:**

// logical AND console.log(true && true); // true console.log(true && false); // false // logical OR console.log(true || false); // true // logical NOT console.log(!true); // false

### Bitwise Operators in JavaScript

Operations on binary representations of numbers are performed by bitwise operators.

Here’s a list of the most common bitwise operators:

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

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise XOR |

~ | Bitwise NOT |

<< | Left shift |

>> | Sign-propagating right shift |

>>> | Zero-fill right shift |

### String Operators in JavaScript

You can also use the + operator in JavaScript to concatenate (join) two or more strings.

**Example of String Operators:**

// concatenation operator console.log('Developers' + 'dome'); // Developersdome let a = 'JavaScript'; a += ' tutorial'; // a = a + ' tutorial'; // JavaScript tutorial console.log(a);

**Note**: The + concatenates(joins) strings when used with them. However, when + is used with numbers, it performs addition.

### Other JavaScript Operators

The following is a list of other JavaScript operators:

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

, | evaluates multiple operands and returns the value of the last operand. | let a = (1, 3 , 4); // 4 |

?: | returns value based on the condition | (5 > 3) ? ‘success’ : ‘error’; // “success” |

delete | deletes an object’s property, or an element of an array | delete x |

typeof | returns a string indicating the data type | typeof 3; // “number” |

void | discards the expression’s return value | void(x) |

in | returns true if the specified property is in the object | prop in object |

instanceof | returns true if the specified object is of of the specified object type | object instanceof object_type |

That’s all about the Operators in JavaScript.

You may like Datatypes in JavaScript.

Hope this article will guide you to recognize the Operators in JavaScript that you needed and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.

Pingback: Type Conversions in JavaScript - Developers Dome