Difference between == and === in JavaScript

While working with values in JavaScript, we often need to compare different values to take some decisions.

JavaScript provides 2 different operators for this purpose:

  1. == (equality operator)
  2. === (strict equality operator)

Both of them are used to check if two values are equal or not. But the way they work is different.

Let's see the difference between them.

Equality operator (==)

The equality operator (==) compares two values without checking their data types and returns true or false accordingly.

For example, if you compare 1 and '1' using the == operator, it will return true because it is not a strict comparison and it converts both values to the same data type before comparing them.

Let's see the example:


// Equality operator (==)
console.log(1 == '1'); // true
console.log(1 == '2'); // false
console.log(1 == true); // true
console.log(0 == false); // true
console.log(0 == ''); // true
console.log(0 == '0'); // true
console.log(1 == 'true'); // false
console.log(0 == 'false'); // false

As you can see in the above example, the equality operator (==) returns true for some values which are not equal in terms of a data type.

For example, 1 and '1' are not equal in terms of data type but if you convert them to string then they become equal.

Strict equality operator (===)

The === operator (strict equality operator) compares two values checking their data types and returns true or false accordingly.

Unlike the equality operator (==), the strict equality operator (===) does not convert the values before comparing them.

If you compare any two values using === operator, and if they are not of the same data type and value, it will return false.

Here are some examples:


// Strict equality operator (===)
console.log(1 ===  1); // true
console.log(1 === '1'); // false
console.log(1 === '2'); // false
console.log(1 === true); // false
console.log(0 === 0); // true
console.log(0 === '0'); // false
console.log(0 === false); // false
console.log(0 === ''); // false

You can see in the above example that the === operator returns true only if both values are of same data type and value.

== vs ===

Here is the table which shows the difference between == and === operator:

OperatorType coercionExampleResult
==Yes1 == '1'true
===No1 === '1'false
==Yestrue == 1true
===Notrue === 1false
==Yesnull == undefinedtrue
===Nonull === undefinedfalse
==Compares values after performing type coercion1 == '1'true
===Compares values without performing type coercion1 === '1'false
When to useWhen NOT to use
When you want to compare values after performing type coercionWhen you want to compare values without performing type coercion
When you want to compare values after converting them to the same typeWhen you want to compare values and their type both should be the same
Easy to understandCan lead to unexpected behavior caused by type coercion
Can be useful in certain scenariosNot recommended for comparing values of different types


So, this is all about the difference between == and === operators in JavaScript. I hope you understood the difference between the two operators.

For more learning check out arithmetic operators in javascript.

Happy learning!😇