How do you write conditional statements in JavaScript, and what operators do conditions use?
You use if / else to run code only when a condition is true, building conditions from comparison operators (===, >, ...) and logical operators (&&, ||, !).
A conditional lets a program make decisions: run one block of code when a condition holds, and (optionally) another when it doesn't.
// Light is bright between 8 and 20 o'clock, otherwise dim
if (zeit >= 8 && zeit <= 20) {
licht = 'hell'; // condition true
} else {
licht = 'schwach'; // condition false
}
(Here zeit is German for "time" and licht for "light" — the logic is what matters.)
Conditions are built from comparison operators, which return a Boolean:
| Operator | Meaning |
|---|---|
== |
Equal (allows type coercion) |
=== |
Strictly equal (value and type, no coercion) |
!= |
Not equal |
> , < |
Greater than / less than |
>= , <= |
Greater-or-equal / less-or-equal |
And logical operators combine several conditions:
| Operator | Meaning |
|---|---|
&& |
AND — both sides must be true |
|| |
OR — at least one side true |
! |
NOT — flips true/false |
A key gotcha: prefer === over ==. The loose == coerces types before comparing, leading to oddities like 0 == '' being true. The strict === compares value and type, which is almost always what you actually want.