Skip to main content

Command Palette

Search for a command to run...

Control Flow in JavaScript: If, Else, and Switch Explained

Updated
8 min read
Control Flow in JavaScript: If, Else, and Switch Explained

When writing JavaScript programs, your code often needs to make decisions just like we do in everyday life. Should something happen or not? Should the program choose one option or another? This is where control flow comes in.

In this blog, we’ll explore how JavaScript uses if, else, and switch statements to control the flow of a program. Don’t worry if you’re new to these concepts everything is explained in a simple and beginner-friendly way with clear examples.

By the end of this post, you’ll understand how to use these statements to make your programs smarter, more dynamic, and easier to read. Let’s dive in and start controlling the flow of our JavaScript code


What Control flow means in Programming.

Control flow simply means how a program decides what code should run and in what order.
Normally, a program runs from a top to bottom line by line. but sometimes we want the program to make decision or repeat actions. control flow helps us do that.
You can think of it like making decisions in daily life. for example -> if it is raining you carry an umbrella , else you go outside normally.

let temperature  = 35;
if(temperature > 30){
    console.log("It is a hot day");
}
else{
    console.log("The weather is pleasant");
}

Now we understand how this code work:
1. The program check the condition temperature > 30.
if the condition is true ,it print " It's a hot day".
if the condition is false, it print "The weather is pleasant".

Here, the program changes its behavior based on the condition, and that decision-making process is called control flow.


The IF statement

The IF statement is used to check a condition in a program. if the condition is true the code inside the if block runs. if the condition is false the code inside the block is skipped
In simple words. the if statement allows a program to make decision based on certain conditions.

if (condition) {
  // code to execute if condition is true
}

condition -> an expression that evaluates to true or false. if the condition is true then the code inside the blue is runs else the condition is ignored
Example :

let age = 20;

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

step-wise :
1. A variable age is created and assigned the value 20.
2. The Program checks the condition (age >= 18) or not.
3. The condition becomes true means the code inside block is runs and we get output You are eligible to vote.

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


The IF - else statement

The if-else statement is used when we want a program to choose between two different actions based on condition.
if the condition is true, the code inside the if block is runs.
if the condition is false, the code inside the else block runs

In simple words, it allows a program to make a decision between two possible outcomes.

if (condition) {
  // code runs if condition is true
} else {
  // code runs if condition is false
}

Example :

let age = 16;

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

Steps:
1. Create a Variable and assign value is 16.
2. The program checks age >= 18 or not in if condition so in this example we get value is less that 18.
3. if value is less than 18 so we go with else condition and output shown "You are not eligible to vote.".


The else if ladder

Sometimes a program needs to check more than one condition. In such cases, we use the else if ladder. it allows the program to test multiple conditions one by one and execute the block of code for the first condition that is true.

You can think of it like climbing a ladder of conditions. The program checks each condition from top to bottom until it finds one that is true.

if (condition1) {
  // code runs if condition1 is true
} else if (condition2) {
  // code runs if condition2 is true
} else if (condition3) {
  // code runs if condition3 is true
} else {
  // code runs if none of the conditions are true
}

Syntax shown how it works
The program checks condition1.

  • If it is true, that block runs and the rest are skipped.

  • If it is false, the program checks the next condition.

  • If none of the conditions are true, the else block runs.

let number = -5;

if (number > 0) {
  console.log("The number is positive.");
} else if (number < 0) {
  console.log("The number is negative.");
} else {
  console.log("The number is zero.");
}

Steps:
1. Create a variable and assign value -5 .
2. First Condition check is number is greater than 0 or not if yes run if block code but in my example it not greater so we can go with next step.
3. Else if condition check is number less than 0 means number is negative and we got our condition is correct here so we go with else if block code and it shown output "The number is negative".
4. else block runs only when the number is exactly 0.


The Switch statement

The switch statement is used when we need to choose one option from many possible options based on a value. It is often used as an alternative to writing many else if condition, making the code cleaner and easier to read.

The Switch statement compares a value with multiple cases. When a match is found the corresponding block of code runs.

switch (expression) {
  case value1:
    // code to run if expression equals value1
    break;

  case value2:
    // code to run if expression equals value2
    break;

  default:
    // code to run if none of the cases match
}

Important Points

  • expression → the value that will be checked

  • case → possible values to match

  • break → stops the switch after a match

  • default → runs if no case matches

let day = 3;

switch (day) {
  case 1:
    console.log("Monday");
    break;

  case 2:
    console.log("Tuesday");
    break;

  case 3:
    console.log("Wednesday");
    break;

  case 4:
    console.log("Thursday");
    break;

  case 5:
    console.log("Friday");
    break;

  case 6:
    console.log("Saturday");
    break;

  case 7:
    console.log("Sunday");
    break;

  default:
    console.log("Invalid day number");
}

steps:
1. Create a variable day and assign the value is 3.
2. The switch statement compares the value of day with each case.
3.The program checks cases and match with case 3 so it print Wednesday , and it will checks all cases one by one and if after check all cases and not find value then it runs with default value.

The switch statement allows a program to select one block of code from multiple options based on a value.


When to use switch vs if-else

Both if-else and switch statements are used to control the flow of a program and make decisions. However, they are useful in different situations. Choosing the right one can make your code cleaner, easier to read, and easier to maintain.

Use if-else When
You should use if-else when the decision depends on conditions, ranges, or multiple logical expressions.
For example, when checking greater than, less than, or multiple conditions.

let marks = 85;

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 75) {
  console.log("Grade B");
} else if (marks >= 50) {
  console.log("Grade C");
} else {
  console.log("Fail");
}

Here we use if-else because we are checking ranges of values, not exact matches

Use switch When
You should use switch when you want to compare one variable with multiple fixed values.
It is especially useful when there are many possible options for the same variable.

let day = 2;

switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  default:
    console.log("Invalid day");
}

Here switch works well because we are matching exact values.


-END-