Loading workspace environment...
Empower programs to make decisions using if, else if, and else branches.
if (condition) { /* runs if true */ }Executes block only if condition evaluates to true.else if (other) { /* runs if other true */ }Checks alternate condition if previous failed.else { /* fallback */ }Default fallback block if all preceding conditions were false.const lightColor = "yellow";
if (lightColor === "green") {
console.log("Safe to go! Drive forward.");
} else if (lightColor === "yellow") {
console.log("Caution! Slow down and prepare to stop.");
} else if (lightColor === "red") {
console.log("Stop immediately! Wait for green.");
} else {
console.log("Unknown signal. Proceed with extreme caution.");
}Caution! Slow down and prepare to stop.if (lightColor === "green")First check evaluates to false.else if (lightColor === "yellow")Second check evaluates to true; block runs.if (score > 90); {
console.log("A grade");
}if (score > 90) {
console.log("A grade");
}Architectural Explanation: Never put a semicolon after the if condition parentheses! That terminates the statement immediately and always runs the block.
const temperature = 15;
if (temperature >= 30) {
console.log("Hot");
} else if (temperature >= 20) {
console.log("Warm");
} else {
console.log("Chilly");
}Create a student score variable (e.g. 72). If score is 90+, print "Honors". If 60-89, print "Passed". Otherwise, print "Needs Retake".