Making a choice
Normally the engine runs every line, in order, top to bottom. A conditional lets a program choose: some lines run only when a condition is true. This script picks what to wear from the temperature.
Run 1: temp = 30
Line 1 stores 30 in temp. Line 2 makes a variable called wear but gives it no value, so it holds undefined: JavaScript's value for "nothing set yet". Line 3 is the decision.
After if comes a condition in round brackets. The engine reads 30 from temp and works out 30 > 25. A comparison always gives a boolean, true or false. Here the answer is true.
The condition is true, so the engine runs the block after it: the lines between the curly braces { and }. Here that is one line, which stores "shorts" in wear.
One block has run, so the engine jumps past the whole else if and else part, straight to line 10. Lines 5 to 9 do not run, and the second condition is never even checked.
console.log(wear) reads wear and writes shorts to the console. Exactly one of the three blocks ran. Now change line 1 to let temp = 18; and run the script again.
Run 2: temp = 18
Now temp holds 18. The engine works out 18 > 25, which is false. A false condition means its block is skipped: line 4 does not run, and the engine moves on to the else.
else if adds another condition. It is checked only because the test above it was false. The engine works out 18 > 10, which is true, so this block runs next.
Line 6 stores "jacket" in wear. The else block is skipped and line 10 prints jacket. Conditions are checked from the top down, and the first true one wins.
Run 3 and the rules
Now temp holds 5, so both tests are false: 5 > 25 and 5 > 10. else has no condition. It runs when every test above it was false, so line 8 stores "coat" and line 10 prints coat.
A condition can use any comparison: === (equal), !== (not equal), <, >, <= or >=, joined with && (and) or || (or). Prefer === to ==, because == converts types first, so 0 == "" is true.
What happens
// step 1/11 · choose conditional code that runs only when a condition is true condition a test, such as temp > 25 result always a boolean: true or false keywords if, else if, else script wear.js (10 lines)
JavaScript has no elseif keyword. else if is simply else followed by another if statement, which is why you can chain as many as you need. The condition does not have to be a comparison. JavaScript turns any value into true or false: false, 0, -0, 0n, "" (empty text), null, undefined and NaN count as false ("falsy"); every other value counts as true.