== vs. ===
& Coercion
Master JavaScript's two equality operators, understand how type coercion works behind the scenes, and learn why strict equality prevents subtle bugs.
What Is Equality in JavaScript?
Every programming language needs a way to compare values. JavaScript gives you two different operators to check if things are "equal":
JavaScript's Type System
Every value in JavaScript has a type. When you use ==, JavaScript may silently convert one type to another. Here are the key primitive types:
What Is Type Coercion?
Type coercion is JavaScript's automatic process of converting a value from one type to another when an operator expects a different type.
The Core Rule
"5" == 5 // true (string → number)
// Strict equality: NO conversion, compares type + value
"5" === 5 // false (string ≠ number)
How Coercion Works with ==
When == encounters different types, it follows these coercion rules in order:
== vs ===: Side by Side
| Expression | == (loose) | === (strict) | Why? |
|---|---|---|---|
| "5" vs. 5 | true | false | String coerced to number |
| 0 vs. false | true | false | Boolean coerced to 0 |
| null vs. undefined | true | false | Special coercion rule |
| "" vs. 0 | true | false | Empty string → 0 |
| 1 vs. 1 | true | true | Same type, same value |
How to Apply This Knowledge
Example 1: String vs Number
let target = 100;
console.log(userInput == target); true
// "100" is coerced to 100, then 100 == 100
console.log(userInput === target); false
// string !== number, no coercion, immediate false
Example 2: Boolean Coercion Trap
console.log("0" == false); true
// Step by step coercion:
// 1. false → 0 (boolean to number)
// 2. "0" → 0 (string to number)
// 3. 0 == 0 → true
// But with strict equality:
console.log("0" === false); false
// string !== boolean, done
Example 3: null vs undefined
console.log(null === undefined); false
console.log(null == 0); false
console.log(null == ""); false
console.log(null == false); false
The Weird Parts of ==
Here are some results that surprise even experienced developers. All of these use ==:
" " == 0 // true (whitespace string → 0)
"" == false // true (both become 0)
"0" == false // true (both become 0)
"" == "0" // false (same type, different values!)
// So: "" == false, "0" == false, but "" != "0"
// Transitivity is BROKEN with ==
Truthy vs Falsy: Related but Different
Coercion also happens in if statements. JavaScript has exactly 6 falsy values:
0
""
null
undefined
NaN
"false"
[] (empty array!)
{} (empty object!)
42, -1
Common Mistakes
Summary: When to Use Which
| Feature | == (loose) | === (strict) |
|---|---|---|
| Type coercion | Yes, automatic | None |
| Predictability | Low: many edge cases | High: type + value must match |
| Performance | Slightly slower (coercion steps) | Slightly faster (no coercion) |
| Best for | null/undefined checks only | Everything else |
| Industry standard | Avoided in most style guides | Recommended default |
Question 1 of 5
What does "3" === 3 evaluate to?
Question 2 of 5
What does null == undefined evaluate to?
Question 3 of 5
Which statement best explains why "0" == false is true?
Question 4 of 5
A developer writes if (x == null) to check if a variable is null or undefined. Is this a good practice?
Question 5 of 5
What is the result of "" == false and "" === false?