


Khusboo Tayal
Control flow in JavaScript determines the sequence in which code is executed. This tutorial will teach you how to control the flow of your program using conditional statements and loops.
Control flow is the order in which individual statements, instructions, or function calls are executed or evaluated in a program. JavaScript, like most programming languages, executes code line by line, but you can alter this order using control flow techniques.
Conditional statements allow you to make decisions in your code based on conditions (true or false). JavaScript provides several types of conditional statements:
Executes a block of code if the condition is true.
if (condition) {
// Code to execute if condition is true
}let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
}Executes one block of code if the condition is true, and another if it is false.
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
let age = 16;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}Handles multiple conditions.
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 75) {
console.log("Grade: B");
} else if (score >= 50) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}A compact way of writing an if...else statement.
condition ? value_if_true : value_if_false;
let isMember = true; let discount = isMember ? "10% off" : "No discount"; console.log(discount); // Output: 10% off
Efficient for checking a variable against multiple values.
switch (expression) {
case value1:
// Code to execute if expression === value1
break;
case value2:
// Code to execute if expression === value2
break;
default:
// Code to execute if none of the cases match
}
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week.");
break;
case "Friday":
console.log("End of the work week.");
break;
default:
console.log("It's a regular day.");
}Loops allow you to execute a block of code multiple times without writing the same code repeatedly.
Runs a block of code for a specified number of times.
for (initialization; condition; update) {
// Code to execute
}for (let i = 1; i <= 5; i++) {
console.log("Iteration: " + i);
}Repeats a block of code as long as the condition is true.
while (condition) {
// Code to execute
}
let count = 1;
while (count <= 5) {
console.log("Count: " + count);
count++;
}Similar to the while loop but guarantees at least one execution because the condition is checked after the code runs.
do {
// Code to execute
} while (condition);let number = 1;
do {
console.log("Number: " + number);
number++;
} while (number <= 5);
Used to iterate over arrays, strings, or any iterable object.
for (variable of iterable) {
// Code to execute
}let fruits = ["Apple", "Banana", "Cherry"];
for (let fruit of fruits) {
console.log(fruit);
}Used to iterate over the properties of an object.
for (key in object) {
// Code to execute
}let person = {
name: "Alice",
age: 30,
country: "USA"
};
for (let key in person) {
console.log(key + ": " + person[key]);
}for (let i = 1; i <= 5; i++) {
if (i === 3) break; // Stops at 3
console.log("Value: " + i);
}
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // Skips 3
console.log("Value: " + i);
}