if (condition) { ... }runs a block of code only when the condition is truthy.else { ... }runs when the condition is falsy. Exactly one path is taken.else iflets you check several conditions in order. The first true one wins, so order matters.- The falsy values are
false,0,-0,0n,"",null,undefinedandNaN. Everything else is truthy (including"0"and"false"). - Combine conditions with
&&,||and!. Always write the full comparison on both sides of||. - Avoid nesting too deeply. Flatten with
else ifand&&where you can. - The ternary operator
condition ? a : bpicks one of two values. switchcompares one value against many exact cases. Don't forgetbreak!
Reading is step one
CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.