The basic notation of control flow in JS involves ()
for any logical condition/expression and {}
to indicate what to do for that logic. Examples are given below.
- if, else if, else
if (condition || alternativeCondition) {
// code to execute if condition is true
} else if (otherCondition && otherCondition2) {
// code to execute if otherCondition is true
} else {
// code to execute if none of the conditions are true
}
- switch
Remember that in the beginning {
is used for separation. However, for cases, we use :
. The values refer to the value of expression
. If none of them are satisfied, default is performed.
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 expression doesn't match any case
}
- for loop
for (initialization; condition; update) {
// code to execute repeatedly while condition is true
}
for (let i = 0; i < 5; i++) {
console.log("Iteration:", i);
}
// Output:
// Iteration: 0
// Iteration: 1
// Iteration: 2
// Iteration: 3
// Iteration: 4
- while loop
while (condition) {
// code to execute repeatedly while condition is true
}
- do…while Loop
do {
// code to execute at least once, and repeatedly while condition is true
} while (condition);
- for…of Loop - This is commonly used with basic iterable data structures such as Arrays, strings, sets, maps, etc.
for (const element of iterable) {
// code to execute for each element in the iterable
}
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
// Output:
// red
// green
// blue
- for…in Loop - This is commonly used for Objects (including arrays, but generally used for object properties)
for (const key in object) {
// code to execute for each key
}
const person = { name: "Alice", age: 30 };
for (const key in person) {
console.log(key, ":", person[key]);
}
// Output:
// name : Alice
// age : 30
- .forEach() method - This method is used to execute a provided function once for each element in an array. Also, it is commonly used with arrow functions.
const numbers = [1, 2, 3, 4];
numbers.forEach((number) => {
console.log(number * 2); // Output: 2, 4, 6, 8
});
- break and continue
- break: Exits the loop or switch statement immediately.
- continue: Skips the current iteration and proceeds to the next iteration of the loop.
for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // exits the loop when i is 5
}
if (i % 2 === 0) {
continue; // skips the current iteration if i is even
}
console.log(i); // prints odd numbers less than 5
}