120+ Engineers
20+ Countries
850+ Projects
750+ Satisfied Clients
4.9 Clutch
120+ Engineers
20+ Countries
850+ Projects
750+ Satisfied Clients
4.9 Clutch
120+ Engineers
20+ Countries
850+ Projects
750+ Satisfied Clients

JavaScript Control Structures: A Beginner’s Guide with Examples

  • Understand if-else, loops, and switch statements in JavaScript

  • Learn syntax and practical examples for each control structure

  • Follow best practices for writing clean, efficient JS code

Last Update: 25 Nov 2024

JavaScript Control Structures: A Beginner’s Guide with Examples image

Types of Control Statements in JavaScript

  • Conditional Statement: These statements are used for decision-making, a decision is made by the conditional statement based on an expression that is passed. Either YES or NO.
  • Iterative Statement: This is a statement that iterates repeatedly until a condition is met. Simply said, if we have an expression, the statement will keep repeating itself until and unless it is satisfied.

In this tutorial, we will explore Conditional Statements and understand their significance in programming. Let's dive in!

if, else if, and else Statement

The if-else statement is one of the most commonly used conditional structures in JavaScript. It evaluates a given condition and executes a block of code if the condition is true. If the condition is false, it can execute a different block of code.

let age = 20;

if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are a minor.");
}
The else if Statement:

Sometimes you may have multiple conditions to check. Instead of nesting multiple if statements, you can use else if to check for additional conditions.

let temperature = 25;

if (temperature > 30) {
    console.log("It's really hot outside!");
} else if (temperature >= 20) {
    console.log("The weather is pleasant.");
} else {
    console.log("It's cold outside.");
}

Here, the program checks the temperature and responds with an appropriate message based on the value of temperature.

Switch case Statement

The switch statement is another way to handle multiple conditions, but it is more suitable when you have a fixed set of conditions (often checking the value of a single variable). It's a cleaner and more readable alternative to multiple if-else statements when you need to compare a single variable against many possible values.

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

In this example, the switch statement checks the value of day and assigns the corresponding day of the week to dayName. The break statement ensures that the switch statement stops after a match is found. If no case matches, the default block is executed.

For loop, while loop, and do-while loop Statement

The for Loop:

The for loop is used when you know the number of iterations in advance. It consists of three parts: initialization, condition, and iteration expression.

for (let i = 0; i < 5; i++) {
    console.log(i);
}

This will print numbers from 0 to 4. The loop starts by initializing i to 0, checks if i is less than 5, and increments i by 1 after each iteration.

Use Case:

Use a for loop when you know the exact number of iterations, such as iterating over an array or performing a task a set number of times.

 

The while Loop:

A while loop is useful when you want to repeat a task an unknown number of times but want to ensure that a condition is checked before each iteration.

let counter = 0;

while (counter < 5) {
    console.log(counter);
    counter++;
}

The loop continues as long as the condition (counter < 5) is true. In this case, it prints numbers from 0 to 4.

Use Case:

Use a while loop when the number of iterations is not known beforehand, and you need to loop as long as a certain condition holds true.

 

The do-while Loop:

let counter = 0;

do {
    console.log(counter);
    counter++;
} while (counter < 5);

This loop will behave the same as the while loop in this case, printing numbers from 0 to 4. However, if the condition were initially false, the do-while loop would still execute once before checking the condition.

Use Case:

Use a do-while loop when you need to ensure that the code inside the loop is executed at least once, even if the condition is false at the start.

 

Conclusion

In JavaScript, both conditional statements (if-else, switch) and looping constructs (for, while, do-while) are fundamental for controlling the flow of your program. Understanding when and how to use each can significantly improve the readability, maintainability, and efficiency of your code. Whether you're making decisions based on conditions or performing repetitive tasks, these constructs give you the flexibility you need to create dynamic and functional applications.

Frequently Asked Questions

Trendingblogs
Get the best of our content straight to your inbox!

By submitting, you agree to our privacy policy.

Have a Project To Discuss?

We're ready!

Let's
Talk