Conditions
Comparison: ===, !==, <, >
Operators that compare two values and answer true or false.
Why it exists
Every decision in a program starts with a comparison: is there enough money in the account, is the password right, is the user over 18, has time run out. The result — true or false — then drives if, loops and filters. So it matters that comparisons are predictable, and that's exactly why JavaScript has strict ===.
Where it's used
- Access checks:
user.role === "admin". - Limits:
balance >= price,password.length < 8. - Searching:
item.id === selectedIdinside a loop or find. - Sorting and filters:
price <= maxPrice.
Syntax
a === b a !== b
a < b a > b
a <= b a >= bExamples
5 === 5; // → true
5 === "5"; // → false — different types
3 !== 4; // → true
2 >= 2; // → trueconst balance = 500;
const price = 450;
balance >= price; // → true
balance - price; // → 50"1" == 1; // → true
"1" === 1; // → false
0 == ""; // → true
0 === ""; // → false== is risky: it converts types"apple" < "pear"; // → true
"Z" < "a"; // → true — capitals come first
"10" < "9"; // → true — strings compare character by characterResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
=== (strict equality) looks at the type first: if the types differ, the answer is immediately false. If they're the same, it compares the values. !== is its opposite.
The old == operator tries to convert the values to a common type first, following complicated rules: "1" == 1, 0 == "", null == undefined are all true. These rules are hard to remember, so modern code almost always uses ===.
<, >, <=, >= work like maths for numbers. Two strings are compared character by character by their character codes — "dictionary order", except that capital letters come before lowercase ones.
Objects and arrays are compared by reference: === answers "is this the same object?", not "do they have the same contents?".