== vs ===
Understand loose equality, strict equality, type coercion, and predictable JavaScript comparisons.
Learn when JavaScript converts values, why production code usually prefers ===, how null and undefined behave, and why objects are compared by reference.
The core difference
== Loose Equality
Compares values and may perform type coercion before comparing them. For example, "5" == 5 is true because the string can be converted to the number 5.
=== Strict Equality
Compares both value and data type without automatic conversion. "5" === 5 is false because a string and a number are different types.
5 == "5" // true
5 === "5" // falseLoose equality converts the string; strict equality sees different types.
true == 1 // true
true === 1 // false
false == 0 // trueLoose equality converts true to 1 and false to 0 before comparing.
null == undefined // true
null === undefined // falseThis is a special rule of loose equality. Strict equality requires the same type.
{} == {} // false
const a = {};
const b = a;
a === b // trueObjects are compared by reference. Two separate object literals are different objects.
Edit the two string inputs. The loose comparison uses JavaScript coercion; the strict comparison checks the strings exactly as entered.
| Expression | == | === | Reason |
|---|---|---|---|
| "5" and 5 | true | false | String converted by == |
| true and 1 | true | false | true becomes 1 |
| false and 0 | true | false | false becomes 0 |
| null and undefined | true | false | Special loose-equality rule |
| {} and {} | false | false | Different object references |
Best Practices and Interview Answer
30-second answer: == performs loose equality and may convert operand types. === performs strict equality by checking both value and type without conversion, so it is usually more predictable in production code.
console.log("10" == 10);
console.log("10" === 10);Work out the result first, then use explicit conversion and strict equality where appropriate.
console.log(false == "");
console.log(false === "");Work out the result first, then use explicit conversion and strict equality where appropriate.
console.log(null == undefined);
console.log(null === undefined);Work out the result first, then use explicit conversion and strict equality where appropriate.
function areEqual(value1, value2) {
return value1 === value2;
}Work out the result first, then use explicit conversion and strict equality where appropriate.
== vs === Quiz
Select an answer to see the explanation.
1. What does == perform when operand types differ?
2. What is the result of "5" === 5?
3. Why is true == 1 true?
4. What is the result of null == undefined?
5. Which operator is preferred for predictable production code?
6. What does {} === {} return?
7. Which expression intentionally checks for both null and undefined?
8. What is the safer way to compare a form age value with 18?
Score: 0 / 8