Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators ⚙️

Updated
•4 min read•View as Markdown

Operators are an important part of programming. In JavaScript operators are symbols or keywords used to perform operations on values and/or variables.

There are various types of operators in JavaScript:

Arithmetic Operators

Used to perform mathematical calculations such as addition, subtraction, division, etc.

Operator Meaning Example
+ Addition: adds 2 numbers 5 + 3 → 8
- Subtraction: subtracts the second number from first one 10 - 3 → 7
* Multiplication: multiplies 2 numbers 4* 3 → 12
/ Division: divides the first number by second number 9 / 3 → 3
% Modulus: also known as remainder operator, returns the remainder left over after dividing first number by second number 5 % 2 → 1

Comparison Operators

Used to compare 2 values and return a boolean, either true or false.

Operator Meaning Example
== Equal to: used to checks values 15 == "15" → true
=== Strict equal: used to check value as well as type 15 === "15" → false because number 15 is not equal to string 15
!= Not equal 5 != 3 → true
> Greater than 8 > 5 → true
< Less than 3 < 9 → true

Difference between == and ===

Eg. if we do, console.log(5 == "5");

Console will print true because it has not checked the type of the value. So, it shows that 5 is equal to 5.

but if, console.log(5 === "5");

Console will print false because now the type of the value has been checked, they are different one is number and another one is string.

Using === is safe because because it checks for strict equality and prevents errors.

Logical Operators

Used to combine or modify, to determine equality or difference between the values.

There are 3 types of logical operators: &&, | |, !

&& returns true if both conditions are true.

Eg. let age = 23;

let canVote = true;

console.log(age> 18 && canVote);

Console prints true since both conditions are satisfied.

| | returns true if at least one condition is true.

Eg. let isWeekend = false;

let isHoliday = true;

console.log(isWeekend || isHoliday);

Console prints true since one condition is true.

! reverses the value.

Eg. let isAdult = true;

console.log(!isAdult);

Console prints false since ! reverses the value.

Assignment Operators

Used to assign values to variables. They can also perform some additional operations as well while assigning a value.

The basic assignment operator is =, which assigns the value.

Eg. If i want to write that value of x is 5, I will write, x = 5.

If i want to add something to x we can use += operator,

Eg. x+=5 which means x = x+ 5;

Similarly we can, subtract to x by writing x-= 5 which means x = x-5.

let x =5;

x+=5;

console.log(x); // output is 10

x-=5;

console.log(x); // output is 5

These operators give a clean look to our code which is easier to read and maintain.

Assignment

Arithmetic operations on two numbers:

const a = 5;

const b= 2;

console.log(a+b); //output is 7

console.log(a-b); //output is 3

console.log(a*b); //output is 10

console.log(a/b); //output is 2.5

Comparing two values using both == and === :

console.log(12=="12"); //output is true

console.log(12==="12"); //output is false

Using logical operators:

let age = 23;

let hasLicense = true;

if (age >= 18 && hasLicense){

console.log("You can drive");

}; //console prints You can drive