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");
}
Explanation
-
ais assigned the value10. - The condition
a > 5is checked. - Since
10is greater than5, the condition is true. - Therefore, the code inside the
ifblock is executed.
Output:
a is greater than 5
if Statement with a False Condition
let a = 10;
if (a < 5) {
console.log("a is greater");
}
Explanation
-
ais assigned the value10. - The condition
a < 5is checked. - Since
10is not less than5, the condition is false. - The code inside the
ifblock 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");
}
Explanation
-
ais assigned the value10. - The condition
a < 5is checked. - Since the condition is false, the
ifblock is skipped. - The
elseblock is executed.
Output:
a is greater
Conclusion
- Use
ifwhen you want code to run only if a condition is true. - Use
if...elsewhen 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");
}
Explanation
-
markis assigned the value90. - The first condition
mark >= 90is checked. - Since
90is equal to90, the condition is true. - The code inside the first
ifblock is executed. - Once a condition becomes true, the remaining
else ifconditions are skipped.
Output:
excellent
How else if Works
The program checks conditions from top to bottom.
- Check
mark >= 90 - If false, check
mark >= 70 - If false, check
mark >= 50 - If all conditions are false, execute the
elseblock
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");
}
Explanation
-
75 >= 90→ false -
75 >= 70→ true - The second block executes.
- Remaining conditions are skipped.
Output:
very good
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");
}
Explanation
-
40 >= 90→ false -
40 >= 70→ false -
40 >= 50→ false - All conditions are false.
- The
elseblock executes.
Output:
improve
Conclusion
- Use
else ifwhen 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
elseblock 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")
}
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)