What is difference between == and === in JS?

Javascript Interview Question #2
Difficulty: EASY
Popularity: 2/5

The difference between == and === in JS is:

==” is the equality operator, which checks if the values on both sides are equal after performing type coercion.

Type coercion means that if the operands are of different types, JavaScript will attempt to convert them to the same type before making the comparison.

===” is the strict equality operator, which also checks if the values on both sides are equal but without performing type coercion.


console.log(5 == "5"); // true, because "5" is coerced to a number and then compared to 5
console.log(5 === "5"); // false, because the types are different

console.log(5 == 5); // true
console.log(5 === 5); // true

console.log(0 == false); // true, because false is coerced to 0
console.log(0 === false); // false, because they are of different types
Best Practice
Use “===” for comparisons to avoid unexpected behavior due to type coercion.

What is Type Coercion in JavaScript and How Does It Work?

difference between == and === in JS

Type coercion in JavaScript refers to the automatic conversion of values from one data type to another when they are used in a context that requires a different type.

Type coercion often occurs implicitly, meaning that JavaScript automatically performs the conversion without requiring explicit instructions from the developer.

Type coercion can sometimes lead to unexpected behavior if not understood properly

Implicit Type Coercion

difference between == and === in JS

Occurs when JavaScript automatically converts values from one type to another during operations like comparisons, arithmetic operations, or concatenations.

For example, when you use the “+” operator with a string and a number, JavaScript will convert the number to a string and concatenate them.

console.log("5" + 3); // "53", because 3 is coerced into a string and concatenated
console.log("5" - 3); // 2, because "5" is coerced into a number and then subtracted from 3
console.log("5" == 5); // true, because "5" is coerced into a number for comparison

Explicit Type Coercion

difference between == and === in JS

Occurs when you explicitly convert values from one type to another using built-in functions or operators like parseInt(), parseFloat(), Number(), String(), etc.

console.log(Number("5")); // 5, converts the string "5" to a number
console.log(String(5)); // "5", converts the number 5 to a string

When to use “==” (double equals) and “===” (triple equals)?

difference between == and === in JS

Use for loose equality comparison when type coercion is acceptableUse for strict equality comparison when strict type checking is required
Check for equality between different types when comparing against null or undefinedEnsure both value and type are identical
difference between == and === in JS

Bottom Line

difference between == and === in JS

Double equals and triple equals are comparison operators.

Use == for loose equality comparisons when type coercion is acceptable, and use === for strict equality comparisons when you need to ensure both value and type are identical.

What to do next?

See Javascript Question #3: “What is event delegation in JS?

Ilyas Seisov

Ilyas Seisov

UI/Web designer and Javascript developer with Master's degree in Computer Science. He helps businesses transform ideas into reality with the power of design and code. Ilyas creates modern, aesthetic web applications and reads minds in a free time.

Leave a Reply

Your email address will not be published. Required fields are marked *