Loading lessons...
Comparison and Logical Operators
Comparison and Logical Operators
Comparison operators compare values and return true or false.
Comparison operators
| Operator | Meaning | Example (x = 5) |
|---|---|---|
== | equal to (loose) | x == 5 true |
=== | equal value and type (strict) | x === 5 true |
!= | not equal (loose) | x != 8 true |
!== | not equal value or type (strict) | x !== 8 true |
> | greater than | x > 3 true |
< | less than | x < 3 false |
>= | greater or equal | x >= 5 true |
<= | less or equal | x <= 4 false |
Loose vs strict
==compares values only, so5 == "5"is true.===compares value and type, so5 === "5"is false.- Always prefer
===to avoid surprises.
Logical operators
| Operator | Meaning | Example | ||||
|---|---|---|---|---|---|---|
&& | AND, true if both are true | a && b | ||||
| ` | ` | OR, true if either is true | `a | b` | ||
! | NOT, flips the result | !a |
Examples
let age = 20;
age >= 18 && age < 65; // true (adult working age)
age < 18 || age > 65; // false
!(age < 18); // true
TL;DR
- Comparison operators return true or false.
- Use
===and!==for safe comparisons. &&AND,||OR,!NOT.