In JavaScript, control constructs (also known as control structures) are essential tools that allow you to direct the flow of execution in a program. Rather than having code execute line by line in a fixed order, control constructs help you perform decision-making, repetitive tasks, and conditional operations. This flexibility enables developers to create dynamic, logical, and interactive applications.
JavaScript provides multiple constructs to control code flow โ mainly for decisions, loops, and logical control. Letโs explore each with practical examples.
Conditional constructs allow a program to execute different blocks of code depending on whether a specific condition is true or false.
- if executes a block when the condition is true.
- else runs when the if condition is false.
- else if allows multiple condition checks in sequence.
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
} else if (age >= 16) {
console.log("You are almost eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
// Output: You are eligible to vote.
The switch statement provides a cleaner alternative to multiple else if conditions. It compares an expressionโs value against multiple cases.
let day = "Tuesday";
switch (day) {
case "Monday":
console.log("Start of the work week!");
break;
case "Tuesday":
console.log("Itโs Tuesday!");
break;
case "Friday":
console.log("Almost weekend!");
break;
default:
console.log("Just another day.");
}
// Output: Itโs Tuesday!
Loops are used to repeat code multiple times until a condition is met. They make repetitive tasks easier and efficient.
The for loop is used when the number of iterations is known in advance. It includes initialization, condition, and increment/decrement parts.
for (let i = 1; i <= 5; i++) {
console.log("Iteration:", i);
}
// Output: Iteration: 1 ... Iteration: 5
Executes a block as long as the given condition remains true.
let count = 1;
while (count <= 5) {
console.log("Count is:", count);
count++;
}
// Output: Count is: 1 ... Count is: 5
The do...while loop executes the block at least once, even if the condition is false, since the condition is checked after the first run.
let number = 1;
do {
console.log("Number:", number);
number++;
} while (number <= 3);
// Output: Number: 1, Number: 2, Number: 3
Used to iterate over iterable objects like arrays, returning each element.
let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
// Output: apple, banana, cherry
Used to iterate over the properties (keys) of an object.
let person = { name: "Alice", age: 25 };
for (let key in person) {
console.log(key + ": " + person[key]);
}
// Output: name: Alice, age: 25
Executes a provided function once for each element in an array.
let colors = ["red", "green", "blue"];
colors.forEach(function(color, index) {
console.log(`Color at index ${index} is ${color}`);
});
// Output:
// Color at index 0 is red
// Color at index 1 is green
// Color at index 2 is blue
Using Arrow Function:
colors.forEach((color, index) => {
console.log(`Color at index ${index} is ${color}`);
});
These statements modify the normal flow of execution in loops or conditionals.
Immediately exits the loop when a certain condition is met.
for (let i = 1; i <= 5; i++) {
if (i === 3) break;
console.log(i);
}
// Output: 1, 2
Skips the current iteration and moves to the next loop cycle.
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
// Output: 1, 2, 4, 5
| Control Construct | Purpose | Example |
|---|---|---|
| If / Else If / Else | Conditional branching | if (age >= 18) |
| Switch | Multi-condition check | switch(day) |
| For Loop | Fixed repetition | for (let i = 0; i < n; i++) |
| While Loop | Conditional repetition | while (condition) |
| Do...While | Execute at least once | do {...} while(condition) |
| For...of | Iterate over values | for (item of array) |
| For...in | Iterate over object keys | for (key in object) |
| forEach | Array iteration | array.forEach() |
| Break | Exit loop early | break; |
| Continue | Skip iteration | continue; |
Mastering control constructs in JavaScript gives you the power to build smarter, more efficient code.
Practice combining conditionals and loops to solve real-world problems efficiently.
Each concept you master strengthens your logical thinking and programming foundation.
Remember: Control constructs are like a map and compass for your code โ they guide the program
exactly where it needs to go. Happy Coding! ๐