Skip to main content

Command Palette

Search for a command to run...

Day 4: Control Flow in JavaScript

Published
3 min read
Day 4: Control Flow in JavaScript

Welcome back to our JavaScript series! Today, we'll learn about control flow statements in JavaScript, which will help us to execute the code based on different conditions.

Control Flow

Control flow refers to the order in which statements are executed in a program. With control flow statements, you can control which blocks of code run and when.

1. if...else Statements

if...else statements let you execute code based on a condition. If the condition is true, the code inside the if block runs; otherwise, the code inside the else block runs.

let age = 18;

if (age >= 18) {
    console.log('You can vote!');
} else {
    console.log('You cannot vote yet.');
}
// Output : You can vote!

2. else if Statements

else if lets you test multiple conditions. If the first condition is false, it checks the next condition.

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');
}
// Output : Grade: B

3. switch Statement

The switch statement is used to perform different actions based on different conditions. It's like a cleaner way to write multiple if...else if statements.

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;
    case 6:
        dayName = 'Saturday';
        break;
    case 7:
        dayName = 'Sunday';
        break;
    default:
        dayName = 'Invalid day';
}

console.log(dayName); // Output: Wednesday

4. Loops

Loops allow you to execute a block of code multiple times.

for Loop

The for loop repeats a block of code a specific number of times.

for (let i = 0; i < 5; i++) {
    console.log('Number: ' + i);
}
// Output: Number: 0, Number: 1, Number: 2, Number: 3, Number: 4
while Loop

The while loop repeats a block of code as long as a specified condition is true.

let count = 0;

while (count < 5) {
    console.log('Count: ' + count);
    count++;
}
// Output: Count: 0, Count: 1, Count: 2, Count: 3, Count: 4
do...while Loop

The do...while loop is similar to the while loop, but it executes the block of code once before checking the condition.The first iteration of do..while loop is always executed.

let num = 0;

do {
    console.log('Number: ' + num);
    num++;
} while (num < 5);
// Output: Number: 0, Number: 1, Number: 2, Number: 3, Number: 4

5. break and continue Statements

The break statement is used to exit from a loop .

The continue statement is used to skips the current iteration and continues with the next iteration.

break Example
for (let i = 0; i < 10; i++) {
    if (i === 5) {
        break;
    }
    console.log('Number: ' + i);
}
// Output: Number: 0, Number: 1, Number: 2, Number: 3, Number: 4
continue Example
for (let i = 0; i < 5; i++) {
    if (i === 2) {
        continue;
    }
    console.log('Number: ' + i);
}
// Output: Number: 0, Number: 1, Number: 3, Number: 4

Summary

Today, we learned about control flow in JavaScript, that includes the use of if...else, switch statements, and loops to control the execution of our code. Tomorrow, we'll dive into functions, which help you organize and reuse your code.

More from this blog

Mohit's blog

51 posts