Episode 15 of 46
Comparison Operators in JavaScript
Learn all comparison operators and the crucial difference between == and ===.
Comparison operators compare two values and return a boolean (true or false).
The Operators
// Equal to
5 == 5 // true
5 == "5" // true (type coercion!)
// Strict equal (recommended)
5 === 5 // true
5 === "5" // false (different types)
// Not equal
5 != 3 // true
5 != "5" // false (type coercion!)
// Strict not equal (recommended)
5 !== "5" // true
5 !== 5 // false
// Greater / Less than
10 > 5 // true
10 < 5 // false
10 >= 10 // true
10 <= 9 // false
== vs === (The Big One)
This is one of the most important concepts in JavaScript:
==(loose equality) — converts types before comparing===(strict equality) — compares both value AND type
// Loose equality — surprises!
0 == false // true
"" == false // true
null == undefined // true
"0" == 0 // true
// Strict equality — predictable!
0 === false // false
"" === false // false
null === undefined // false
"0" === 0 // false
Always use === and !== to avoid unexpected behavior.
Comparing Strings
"apple" < "banana" // true (alphabetical)
"a" < "b" // true
"Z" < "a" // true (uppercase before lowercase)