javascript Tutorial

JavaScript Operators

Learn more about one of the world’s most popular programming languages.

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.

OperatorExample
+  (Addition) 3 + 5 
–   (Subtraction)10 – 4
*  (Multiplication)7 * 5
/  (Division)8 / 2
** (exponent or power)2 ** 4
% (modulo or remainder)8 % 4

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
OperatorExample
++ (increment the value by 1)let count = 0;count++;++count;
— (decrement the value by 1)let count = 0;count–;–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.

OperatorExample
= (Assignment)let count = 0;
+= (Addition Assignment)let count = 0;count += 5; // equivalent to count = count + 5;console.log(count) // displays 5
-= (Subtraction Assignment)let count = 5;count -= 5; // equivalent to count = count – 5;console.log(count) // displays 0
*= (Multiplication Assignment)let count = 1;count *= 5; // equivalent to count = count * 5;console.log(count) // displays 5
/= (Division Assignment)let count = 10;count /= 5; // equivalent to count = count / 5;console.log(count) // displays 2

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.

OperatorExample
== (non strict equality)0 == 0 // true0 == false // true as both values are falsy
=== (strict equality)0 === 0 // true0 === false // false as types are different
!= (non strict not equal)1 != 0 // true1 != true // false as both values are truthy
!== (strict not equal)1 != 0 // true1 !== true // true as both types are different
> (greater than)5 > 4 // true
>= (greater than or equal to)5 >= 5 // true
< (less than)7 < 10 //true
<= (less than or equal to)7 <= 7 //true

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
  • No downloads or setup required