Lesson 2 of 4 · 8 minutes

Otherwise: if / else

Often you want one action when a condition is true and a different action when it is false. That is what else is for.

If the condition is true, the if block runs. If it is false, the else block runs instead.

Exactly one block runs. Never both. Never neither. It is like a fork where both paths lead somewhere.

A condition sends the path to one of two messages

Exactly one path is always taken.

Loading the editor…

Add console.log("Too young to vote"); inside else. Then test ages 15 and 20.

Quick check

With if and else, how many blocks run?

Choose one answer.

The condition is true. Does the else block run?

Choose one answer.

You can now handle both outcomes of a decision.