Obafemi Emmanuel

Mastering Control Flow & Loops in JavaScript

Published 2 months ago

Introduction

JavaScript is a powerful language used to create dynamic web applications. One of its essential aspects is control flow, which dictates how statements are executed. This includes conditional statements and loops, which allow us to make decisions and repeat actions efficiently. In this guide, we will explore JavaScript's control flow mechanisms, including conditional statements, loops, and the use of break and continue.


1. Conditional Statements

Conditional statements help control the execution of code based on certain conditions. JavaScript provides multiple ways to handle conditions:


1.1 If Statement

The if statement allows you to execute a block of code only if a specified condition evaluates to true.

let age = 18;
if (age >= 18) {
    console.log("You are eligible to vote.");
}

1.2 If-Else Statement

The if-else statement provides an alternative block of code that executes if the condition is false.

let temperature = 30;
if (temperature > 25) {
    console.log("It's a hot day.");
} else {
    console.log("It's a cool day.");
}

1.3 Else If Statement

The else if statement allows multiple conditions to be checked sequentially.

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");
}

1.4 Switch Statement

The switch statement is used when multiple values need to be compared efficiently.

let day = "Monday";
switch (day) {
    case "Monday":
        console.log("Start of the work week.");
        break;
    case "Friday":
        console.log("Weekend is near!");
        break;
    default:
        console.log("It's a regular day.");
}

2. Loops in JavaScript

Loops are used to execute a block of code multiple times until a condition is met.


2.1 For Loop

A for loop is commonly used when the number of iterations is known.

for (let i = 1; i <= 5; i++) {
    console.log("Iteration: " + i);
}

2.2 While Loop

A while loop executes as long as the condition remains true.

let count = 1;
while (count <= 5) {
    console.log("Count: " + count);
    count++;
}

2.3 Do...While Loop

A do...while loop ensures that the block of code runs at least once before checking the condition.

let num = 1;
do {
    console.log("Number: " + num);
    num++;
} while (num <= 5);

2.4 For...Of Loop

The for...of loop iterates over iterable objects such as arrays.

let fruits = ["Apple", "Banana", "Cherry"];
for (let fruit of fruits) {
    console.log(fruit);
}

2.5 For...In Loop

The for...in loop iterates over the properties of an object.

let student = {name: "John", age: 20, grade: "A"};
for (let key in student) {
    console.log(key + ": " + student[key]);
}

3. Break and Continue Statements

3.1 Break Statement

The break statement terminates a loop when a certain condition is met.

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

3.2 Continue Statement

The continue statement skips the current iteration and proceeds with the next one.

for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue; // Skips iteration when i equals 3
    }
    console.log(i);
}

Conclusion

Control flow statements and loops are fundamental to writing efficient JavaScript code. By understanding conditional statements (if, else, else if, switch) and loops (for, while, do...while, for...of, for...in), along with break and continue, you can create more dynamic and interactive applications.

Practice these concepts with real-world examples to master them effectively!


Leave a Comment


Choose Colour