Comparison and Logical Operators
=== and !== for comparing values; && and || for combining conditions
= and === — not the same thing
The most common beginner mistake: confusing = and ===. = (one equals sign) — ASSIGNMENT: put a value into a variable. === (three equals signs, read as 'strict equal') — COMPARISON: check whether two values are equal. The result of === is always true or false — nothing else.
Three = signs — not a typo
In JavaScript there are three equals and they're different:
- = (one) — assignment: write a value into a variable
- == (two) — loose equality: compares values but converts types
- === (three) — strict equality: compares both value and type
Also two 'not equal':
- != (one) — loose 'not equal', converts types
- !== (two) — strict 'not equal', safe to use
Rule: always use === and !==. Never == and != — they give unexpected results.
All comparison operators
All return true or false:
| Operator | Reads as | Example | Result |
|---|---|---|---|
| === | strict equal | 5 === 5 | true |
| !== | strict not equal | 5 !== 3 | true |
| > | greater than | 10 > 3 | true |
| < | less than | 2 < 1 | false |
| >= | greater or equal | 5 >= 5 | true |
| <= | less or equal | 4 <= 3 | false |
Why === and not ==? See: "5" == 5 gives true — JS decided the string and number are the same. "5" === 5 gives false — correct, they're different types.
=== checks both value and type — no surprises.
Logical operators
Allow combining multiple conditions:
| Operator | Meaning | Example | Result |
|---|---|---|---|
&& | AND — both conditions | age >= 18 && age < 65 | true |
|| | OR — at least one | age < 18 || age > 65 | false |
! | NOT — inverts | !true | false |
Rule &&: result is true only if BOTH conditions are true.
Rule ||: result is true if at least ONE condition is true.
Logical operators are the heart of any condition in code.
Remember the order: first ! (NOT), then && (AND), then || (OR). Like math: first multiplication, then addition. Parentheses always help make code clearer.