Javascript - if/else structure
📖 Read the lesson
Code Life
By Pierre Marsaa
The else Branch
With
if
alone, something happens... or nothing at all. But often you want a
plan B
: attack OR defend, win OR lose. That's the job of
else
.
↗️
Loading editor...
↶
▶️ Run
↷
The condition is
false
(12 is not greater than 30), so the
else
block runs. Change the HP to
50
and see the other branch run instead.
No Condition on else
else
never has its own condition. It simply means: "in all other cases". Writing
else
(hp <
30
)
is a syntax error.
Verify