Control Flow
Making decisions and repeating things — if/else, switch, for, while, for...of, and the for...in trap.
switch compares with strict equality (===), so a case only matches a value of the same type. Values from a form field, prompt() or a URL are always strings. A switch written with numeric cases never matches them, and every choice falls through to default, without an error.
function planName(choice) {
switch (choice) {
case 1: return "Basic";
case 2: return "Pro";
default: return "Unknown plan";
}
}
const fromForm = "2"; // <select> and <input> values are always strings
console.log(planName(fromForm)); // Unknown planMaking decisions and repeating things
By default, JavaScript runs your code from top to bottom, one line at a time. Control flow statements let you change that — make decisions about which code to run, and repeat code without writing it multiple times.
if / else — making a decision
let temperature = 28;
if (temperature > 30) {
console.log("It's hot! Stay hydrated.");
} else if (temperature > 20) {
console.log("Nice weather.");
} else {
console.log("It's cold. Bring a jacket.");
}
// Prints: "Nice weather."How it works: JavaScript checks the condition (the part in parentheses). If it's
true, it runs the code in the first { } block and skips the rest.
If it's false, it tries the next else if, and so on.
The final else runs if nothing else matched.
The ternary operator — if/else in one line
For simple yes/no decisions, the ternary is often cleaner:
// condition ? value-if-true : value-if-false
const age = 20;
const label = age >= 18 ? "adult" : "minor";
console.log(label); // "adult"
// Same as:
let label2;
if (age >= 18) {
label2 = "adult";
} else {
label2 = "minor";
}switch — multiple fixed options
Use switch when you're comparing one value against several fixed options:
const day = "Monday";
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
console.log("Weekday");
break; // ← important! without break, it "falls through" to the next case
case "Saturday":
case "Sunday":
console.log("Weekend");
break;
default:
console.log("Unknown day");
}break, JavaScript falls through to
the next case and runs that code too — a common bug. The only time to omit break
intentionally is to share code between cases (like the weekday example above).
The switch above works because day and its cases are both strings. When the value can arrive as text, convert it once before the switch, or write the cases as the type you will actually receive:
const fromForm = "2";
console.log(planName(Number(fromForm))); // Profor loop — repeat a set number of times
// Structure: for (start; condition; step)
for (let i = 0; i < 5; i++) {
console.log(`Step ${i}`); // Step 0, Step 1, Step 2, Step 3, Step 4
}
// Countdown:
for (let i = 10; i > 0; i--) {
console.log(i); // 10, 9, 8 ... 1
}
console.log("Blast off!");for...of — loop over arrays (the right way)
for...of gives you each VALUE in an array, one at a time.
Use this for arrays.
const cities = ["Mumbai", "Delhi", "Bangalore"];
for (const city of cities) {
console.log(`City: ${city}`);
}
// City: Mumbai
// City: Delhi
// City: Bangalore
// Works on strings too:
for (const char of "hello") {
console.log(char); // h, e, l, l, o
}while — repeat while a condition is true
let lives = 3;
while (lives > 0) {
console.log(`Lives remaining: ${lives}`);
lives--; // decrease by 1 each time
}
console.log("Game over!");
// Lives remaining: 3
// Lives remaining: 2
// Lives remaining: 1
// Game over!break and continue
// break — exit the loop immediately
for (let i = 0; i < 10; i++) {
if (i === 5) break; // stop when i hits 5
console.log(i); // 0, 1, 2, 3, 4
}
// continue — skip this iteration, move to the next
for (let i = 0; i < 6; i++) {
if (i % 2 === 0) continue; // skip even numbers
console.log(i); // 1, 3, 5
}Official Sources
- MDN Web Docs — Control flow and error handling
- MDN Web Docs — Loops and iteration
- MDN Web Docs — for...of
- MDN Web Docs — for...in
- ECMAScript 2024 — Iteration (§27.1)
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.