JavaScript Interview Question #10

What is the difference between == and === in JavaScript?

JavaScript Fundamentals, Types, Scope & Functions Junior Beginner

Quick Interview Answer

The == operator performs loose equality comparison and may convert operands to compatible types before comparing them. The === operator performs strict equality and does not perform this type coercion.

Detailed Explanation

The == operator performs loose equality comparison and may convert operands to compatible types before comparing them. The === operator performs strict equality and does not perform this type coercion.

For predictable application code, strict equality is normally preferred. Loose equality can be useful in specific cases when its coercion rules are intentionally understood, but it can otherwise produce surprising results.

Code Example

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

console.log(false == 0);  // true
console.log(false === 0); // false