javascript Tutorial

# JavaScript Operators

Developers often have to write code that does mathematical and logical calculations. JavaScript provides a very comprehensive list of operators which can be used to perform such tasks. Operators in JavaScript are broadly divided as:

• Arithmetic Operators
• Increment/Decrement Operators
• Assignment Operators
• Comparison Operators
• Other Operators

## JavaScript Arithmetic Operators

An arithmetic operator in JavaScript is used to perform arithmetic on numbers (the numbers can be variables, literals, or expressions). Arithmetic operations typically operate on two numbers.

## Increment & Decrement Operators in JavaScript

Increment (two plus sign) `++` & Decrement (two minus sign) `--` Operators are used to increase or decrease the value of a variable by 1. Depending on whether the operator sign is used before or after the variable, it produces a different result. However, it still increments or decrements by 1.

Code Example
``````let count = 0;
console.log(count); // prints 0
count++; // first assign the current value back to count and then increase it by 1
++count; // first increase the value by 1 and then assign the new value to count``````

## JavaScript Assignment Operators

Assignment operator or equal sign `=` is used for assigning the value on the right-hand side of the equal sign to the variable on the left-hand side of the equal sign. It is the most commonly used operator in JavaScript as there is always a need for assigning the value to a variable.

Increment and Decrement Operators also have a shorthand notation that uses the assignment operator. While increment operator `++` and decrement operator `--` is used to increase or decrease the value by 1, sometimes there is a need to increase or decrease the value by a different amount. In this case a variation of increment `+=` and decrement `-=` is used.

While writing code in JavaScript, there is always a need to make logical decisions in code. Comparison operators are often used while writing logical statements. They compare two values and return a boolean value either `true` or `false`. All the values are either truthy or falsy in JavaScript and hence using comparison operators always returns a true or false value.

## JavaScript Comparison Operators

One key thing to note here is that JavaScript allows both non-strict `==` and strict `===` comparison of two values. It is highly recommended that Developers in JavaScript always use triple equal sign `===` or strict equality comparison. `==` sign compares only the values to be either truthy or falsy while `===` compares both the values and as well the type of both the values to be equal for a more strict comparison.

## Other JavaScript Operators

Here are some of the other operators available in JavaScript:

### JavaScript Bitwise Operators

Bitwise operators are used to perform operations on binary representations of numbers. Bitwise operators convert any numeric operand in the operation into a 32-bit number, before returning a standard JavaScript number.

### JavaScript Logical Operators

Logical operators in JavaScript allow a program to make a decision based on multiple conditions, where each operand can evaluate to a true or false value.

### JavaScript Ternary Operators

The ternary or conditional operator in JavaScript can be used to simplify an if…else statement. A ternary operator performs an evaluation on a condition then executes a block of code based on that condition, and as a result, takes three operands.

``const whatToWear = isWeekend ? “jeans”: “suit”;  ``

### JavaScript Typeof Operators

The typeof operator in JavaScript returns a string indicating the type of the unevaluated operand.

## Learn JavaScript Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

• Free and fun
• Designed for beginners