Control flow determines the order in which individual statements, instructions, or function calls are executed or evaluated in a program. JavaScript provides several control flow statements that allow you to make decisions, execute code conditionally, and perform repetitive tasks. (Javascript Cheatsheet, Medium)
if
StatementThe if
statement executes a block of code if a specified condition is true.(DEV Community)
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
}
if...else
StatementThe if...else
statement executes one block of code if a condition is true, and another block if the condition is false.(DEV Community)
Syntax:
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
Example:
let age = 16;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
if...else if...else
StatementThe if...else if...else
statement allows you to test multiple conditions.(DEV Community)
Syntax:
if (condition1) {
// code if condition1 is true
} else if (condition2) {
// code if condition2 is true
} else {
// code if none of the conditions are true
}
Example:
let score = 85;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 80) {
console.log("Grade B");
} else {
console.log("Grade C");
}
switch
StatementThe switch
statement evaluates an expression and executes code blocks based on matching case labels.(web.dev)
Syntax:
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// default code block
}
Example:
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Another day");
}
for
LoopThe for
loop repeats a block of code a specified number of times.(DEV Community)
Syntax:
for (initialization; condition; increment) {
// code block to be executed
}
Example:
for (let i = 0; i < 5; i++) {
console.log("Iteration:", i);
}
while
LoopThe while
loop executes a block of code as long as a specified condition is true.(DEV Community)
Syntax:
while (condition) {
// code block to be executed
}
Example:
let i = 0;
while (i < 5) {
console.log("Iteration:", i);
i++;
}
do...while
LoopThe do...while
loop is similar to the while
loop, but it executes the code block once before checking the condition.(Wikipedia)
Syntax:
do {
// code block to be executed
} while (condition);
Example:
let i = 0;
do {
console.log("Iteration:", i);
i++;
} while (i < 5);
for...in
LoopThe for...in
loop iterates over the enumerable properties of an object.(Wikipedia)
Syntax:
for (let key in object) {
// code block to be executed
}
Example:
const person = { name: "Alice", age: 25 };
for (let key in person) {
console.log(key + ": " + person[key]);
}
for...of
LoopThe for...of
loop iterates over iterable objects like arrays, strings, etc.(Medium)
Syntax:
for (let value of iterable) {
// code block to be executed
}
Example:
const numbers = [1, 2, 3];
for (let num of numbers) {
console.log(num);
}
break
StatementThe break
statement terminates the current loop or switch
statement.(GeeksforGeeks)
Example:
for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
continue
StatementThe continue
statement skips the current iteration of a loop and continues with the next iteration.
Example:
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue;
}
console.log(i);
}
if
and switch
statements?for
loop differ from a while
loop?break
and continue
statements?for...in
and for...of
loops differ?do...while
loop work?while
loop to calculate the sum of numbers from 1 to 100?do...while
loop to prompt a user until they enter the correct password?These questions and answers should provide a solid understanding of control flow in JavaScript and prepare you for related interview topics.
if
, else if
, and else
to execute code blocks based on conditions.switch
for multiple condition checks against a single expression.for
, while
, do...while
) to execute code repeatedly.for...in
to iterate over object properties.for...of
to iterate over iterable objects like arrays.break
to exit loops or switch
statements prematurely.continue
to skip the current iteration in loops.(Medium, Codeguage, Wikipedia)Understanding and effectively using control flow statements is fundamental to writing efficient and logical JavaScript code.
Feel free to reach out if you have any questions or need further clarification on any of these topics!