Control Flow in JavaScript: if, else, else if, and switch
In programming, we often need our code to make decisions.
For example:
If a user is 18 or older, allow them to register.
If a student scores above 90, assign grade A.
If a user selects a menu option, perform a specific action.
Just like in real life, programs also need to choose between different paths. This concept is called control flow.
In this article, we’ll learn the most common decision-making tools in JavaScript:
ifstatementif...elsestatementelse ifladderswitchstatementWhen to use
switchvsif-else
All examples are kept simple and beginner-friendly, so you can try them in your browser console or Node.js.
What is Control Flow?
Control flow refers to the order in which the program executes instructions.
Normally, code runs from top to bottom.
Example:
console.log("Start");
console.log("Learning JavaScript");
console.log("End");
Output:
Start
Learning JavaScript
End
But sometimes we want the program to run certain code only if a condition is true. That’s where control flow statements like if and switch come in.
The if Statement
The if statement runs a block of code only if a condition is true.
Syntax
if (condition) {
// code to run if condition is true
}
Example: Checking Age
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
}
Step-by-step execution
JavaScript checks the condition →
age >= 1820 >= 18is trueThe code inside the
ifblock runs.
Output:
You are eligible to vote.
If the condition were false, nothing would run.
The if...else Statement
Sometimes we want two possible outcomes.
For example:
If a person is 18 or older → allow access
Otherwise → deny access
Syntax
if (condition) {
// runs if condition is true
} else {
// runs if condition is false
}
Example
let age = 16;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("You cannot vote yet.");
}
Step-by-step execution
Check condition →
16 >= 18Condition is false
JavaScript runs the else block
Output:
You cannot vote yet.
The else if Ladder
Sometimes there are multiple conditions to check.
Example: Assigning grades based on marks.
Example
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");
}
Step-by-step execution
JavaScript checks conditions from top to bottom:
85 >= 90→ false85 >= 75→ true
Once a condition becomes true, the remaining conditions are not checked.
Output:
Grade B
The switch Statement
The switch statement is another way to make decisions when we have multiple possible values for the same variable.
Syntax
switch (value) {
case option1:
// code
break;
case option2:
// code
break;
default:
// code
}
Example: Menu Selection
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the work week");
break;
case "Friday":
console.log("Almost weekend");
break;
case "Sunday":
console.log("Weekend!");
break;
default:
console.log("Regular day");
}
Output:
Start of the work week
Why break is Important in switch
The break statement stops the switch block after a match is found.
Without break, JavaScript will continue executing the next cases.
Example without break:
let number = 1;
switch (number) {
case 1:
console.log("One");
case 2:
console.log("Two");
}
Output:
One
Two
This happens because there is no break after case 1.
Correct version:
switch (number) {
case 1:
console.log("One");
break;
case 2:
console.log("Two");
break;
}
Output:
One
So always remember:break prevents execution from continuing into other cases.
When to Use switch vs if-else
Use if-else when:
Conditions involve ranges
Conditions involve different variables
Logical operators are used (
&&,||)
Example:
if (marks > 90)
Use switch when:
Checking one variable
Comparing specific values
Multiple possible options exist
Example:
switch(day)
Example Comparison
Using if-else:
if (day === "Monday") {
console.log("Start of week");
}
Using switch:
switch (day) {
case "Monday":
console.log("Start of week");
break;
}
Both work, but switch is often cleaner when there are many options.
Control flow allows your program to make decisions and behave differently depending on conditions.
The most commonly used tools are:
if→ run code if a condition is trueif...else→ choose between two outcomeselse if→ check multiple conditionsswitch→ handle multiple possible values
