8  Control Flow

8.1 Conditional Statements

8.1.1 if/else Statement

let score = 85;

if (score >= 90) {
    console.log("Grade: A");
} else if (score >= 80) {
    console.log("Grade: B");
} else if (score >= 70) {
    console.log("Grade: C");
} else {
    console.log("Grade: F");
}
Grade: B
if (true) console.log("Hello, World!"); // Single-line if statement
Hello, World!

8.1.2 Ternary Conditional Operator

A compact form for simple if/else situations:

condition ? expressionIfTrue : expressionIfFalse
let age = 19;
let status = age >= 18 ? "adult" : "minor";
console.log(status);
adult

8.1.3 switch Statement

let day = 3;
let dayName;

switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    case 4:
        dayName = "Thursday";
        break;
    case 5:
        dayName = "Friday";
        break;
    default:
        dayName = "Weekend";
}

console.log(dayName); 
Wednesday

Note the importance of the break statement - without it, execution “falls through” to the next case. This can be useful in some scenarios but is usually a source of bugs.

8.2 For Loops

8.2.1 traditional for Loop

The traditional counting loop:

// for (initialization; condition; update)
for (let i = 0; i < 5; i++) {
    console.log(i);
}
0
1
2
3
4

8.2.2 for...of Loop (ES6)

This is conceptually similar to Python’s for item in list syntax.

const fruits = ["apple", "banana", "cherry"];

for (const fruit of fruits) {
    console.log(fruit);
}
apple
banana
cherry

8.2.3 for...in Loop

Used to iterate over the properties of an object:

const person = {
    name: "John",
    age: 30,
    job: "developer"
};

for (const key in person) {
    console.log(`${key}: ${person[key]}`);
}
name: John
age: 30
job: developer

Be careful: for...in loops also iterate over inherited properties. When working with arrays, it’s generally better to use for...of or a standard for loop.

8.2.4 Loop with key and values (.entries())

const arr = [1, 2, 3, 4]

for (let [i, value] of arr.entries()) {
  console.log(i, value)
}
0 1
1 2
2 3
3 4

8.3 while Loop

Executes as long as a condition is true:

let count = 0;

while (count < 5) {
    console.log(count);
    count++;
}
0
1
2
3
4
4

8.3.1 do...while Loop

let count = 0;

do {
    console.log(count);
    count++;
} while (count < 5);
0
1
2
3
4
4

Even if the condition is initially false, the code block executes once:

let count = 10;

do {
    console.log("This runs once even though count > 5");
} while (count < 5);
This runs once even though count > 5

8.4 Loop Control

JavaScript provides statements to control loop execution:

8.4.1 break Statement

Terminates the current loop immediately:

for (let i = 0; i < 10; i++) {
    if (i === 5) {
        break; // Exit the loop when i equals 5
    }
    console.log(i);
}
0
1
2
3
4

8.4.2 continue Statement

Skips the current iteration and continues with the next one:

for (let i = 0; i < 10; i++) {
    if (i % 2 === 0) {
        continue; // Skip even numbers
    }
    console.log(i);
}
1
3
5
7
9

8.4.3 labeled Statements

Less commonly used, but allows you to break out of nested loops:

outerLoop: for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 3; j++) {
        if (i === 1 && j === 1) {
            break outerLoop; // Breaks out of both loops
        }
        console.log(`i=${i}, j=${j}`);
    }
}
i=0, j=0
i=0, j=1
i=0, j=2
i=1, j=0