DEV Community

Guna Ramesh
Guna Ramesh

Posted on

JavaScript Control Flow Statements

JavaScript Control Flow Statements

Control flow statements help us decide which code should run based on conditions.

if Statement

The if statement executes a block of code only when the condition is true.

let a = 10;

if (a > 5) {
    console.log("a is greater than 5");
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • a is assigned the value 10.
  • The condition a > 5 is checked.
  • Since 10 is greater than 5, the condition is true.
  • Therefore, the code inside the if block is executed.

Output:

a is greater than 5
Enter fullscreen mode Exit fullscreen mode

if Statement with a False Condition

let a = 10;

if (a < 5) {
    console.log("a is greater");
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • a is assigned the value 10.
  • The condition a < 5 is checked.
  • Since 10 is not less than 5, the condition is false.
  • The code inside the if block does not execute.
  • No output is displayed.

if...else Statement

The else block is used when the if condition is false.

let a = 10;

if (a < 5) {
    console.log("a is smaller");
} else {
    console.log("a is greater");
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • a is assigned the value 10.
  • The condition a < 5 is checked.
  • Since the condition is false, the if block is skipped.
  • The else block is executed.

Output:

a is greater
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • Use if when you want code to run only if a condition is true.
  • Use if...else when you want one block to run if the condition is true and another block to run if the condition is false.
  • Control flow statements make programs intelligent by allowing them to make decisions.

JavaScript else if Statement

The else if statement is used when we need to check multiple conditions.

let mark = 90;

if (mark >= 90) {
    console.log("excellent");
}
else if (mark >= 70) {
    console.log("very good");
}
else if (mark >= 50) {
    console.log("good");
}
else {
    console.log("improve");
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • mark is assigned the value 90.
  • The first condition mark >= 90 is checked.
  • Since 90 is equal to 90, the condition is true.
  • The code inside the first if block is executed.
  • Once a condition becomes true, the remaining else if conditions are skipped.

Output:

excellent
Enter fullscreen mode Exit fullscreen mode

How else if Works

The program checks conditions from top to bottom.

  1. Check mark >= 90
  2. If false, check mark >= 70
  3. If false, check mark >= 50
  4. If all conditions are false, execute the else block

Example 2

let mark = 75;

if (mark >= 90) {
    console.log("excellent");
}
else if (mark >= 70) {
    console.log("very good");
}
else if (mark >= 50) {
    console.log("good");
}
else {
    console.log("improve");
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • 75 >= 90 → false
  • 75 >= 70 → true
  • The second block executes.
  • Remaining conditions are skipped.

Output:

very good
Enter fullscreen mode Exit fullscreen mode

Example 3

let mark = 40;

if (mark >= 90) {
    console.log("excellent");
}
else if (mark >= 70) {
    console.log("very good");
}
else if (mark >= 50) {
    console.log("good");
}
else {
    console.log("improve");
}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • 40 >= 90 → false
  • 40 >= 70 → false
  • 40 >= 50 → false
  • All conditions are false.
  • The else block executes.

Output:

improve
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • Use else if when you need to check multiple conditions.
  • Conditions are checked from top to bottom.
  • As soon as one condition becomes true, its block is executed.
  • The remaining conditions are ignored.
  • If no condition is true, the else block is executed.

switch statement

let day = 2;
        switch (day) {
            case 1:
                console.log("sunday")
                break;
            case 2:
                console.log("monday")
                break;
            case 3:
                console.log("tuesday")
                break;
            case 4:
                console.log("wednesday")
                break;
            case 5:
                console.log("thursday")
                break;
            case 6:
                console.log("friday")
                break;
            case 7:
                console.log("saturday")
                break;
            default:
                console.log("invalid")

        }
Enter fullscreen mode Exit fullscreen mode

Explanation
day is assigned the value 2.
The switch statement checks the value of day.
It compares day with each case.
case 2 matches the value.
The code inside case 2 is executed.
The break statement stops the execution of the switch block.

Output:

monday

Top comments (0)