Last Updated : 02 Aug, 2025
Control flow statements in JavaScript control the order in which code is executed. These statements allow you to make decisions, repeat tasks, and jump between parts of a program based on specific conditions.
JavaScript if StatementThe if statement executes a block of code only if a specified condition is true.
if statement JavaScript
const age = 18;
if (age >= 18) {
console.log("You are an adult.");
}
The if...else statement provides an alternate block of code to execute if the condition is false.
if else statement JavaScript
const score = 40;
if (score >= 50) {
console.log("You passed.");
} else {
console.log("You failed.");
}
The if...else if...else statement is used when you want to handle multiple conditions.
JavaScript
const temp = 25;
if (temp > 30) {
console.log("It's hot.");
} else if (temp >= 20) {
console.log("It's warm.");
} else {
console.log("It's cold.");
}
The switch statement evaluates an expression and executes a block of code based on matching cases. It provides an alternative to long if-else chain.
JavaScript
const day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week.");
break;
case "Friday":
console.log("End of the workweek.");
break;
default:
console.log("It's a regular day.");
}
Start of the week.
In some programming languages, a ternary operator is used to assign a value to a variable based on a condition.
JavaScript
let a = 10;
console.log(a === 5 ? "a is equal to 5" : "a is not equal to 5");
Output
a is not equal to 5
let a = 10;
assigns 10
to variable a
.a === 5 ? "a is equal to 5" : "a is not equal to 5";
checks if a
is strictly equal to 5
.
"a is equal to 5"
."a is not equal to 5"
.Control flow statements are backbone in programming for
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4