If/Else Statements
if and else are the everyday tools for choices.
You ask a question. Then you pick a path.
Think of checking the cookie jar. If cookies are there, eat one. Else, wait.
Simple if
if else
else if chains
Nested if
Ternary shortcut
Simple if
Write if (condition) { ... }.
The block runs only when the condition is true.
if else
else covers the other path.
Exactly one of the two blocks will run.
else if
Chain more tests when you have several options.
JavaScript checks from the top. It stops at the first true one.
Nested if
You can put an if inside another if.
Do it only when it stays easy to read.
Ternary operator
A tiny shortcut looks like condition ? yesValue : noValue.
Use it for small choices, not big blocks.
Tip: Indent carefully. Matching braces make nested choices much easier to follow.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.