DEV Community

Daniel Joo
Daniel Joo

Posted on • Updated on

Difference between == and === in JS

Anyone new to Javascript can at one point find themselves asking this very question when dealing with these comparison operators. This short answer is this: == requires that the variables on either side are of the same value, while === requires both variables to be of the same value AND the same variable type.

For ==, type coercion is performed. Type coercion is when the variable types for both variables are set to the same type before the values are compared. Here is an example of all this:

const variable = '3'
const variable2 = 3

console.log(variable == variable2)
// true
console.log(variable === variable2) 
// false 

In this code, the first console log statement returns true. This is because type coercion makes both variables to have the same type. The second console log statement returns false because although both variables have the same value, variable is a string, while variable2 is a number.

Top comments (0)