JavaScript control flow refers to the order in which statements are executed in a JavaScript program. It determines how the program progresses from one statement to another based on certain conditions and control structures.
Javascript Control Flow
Control flow in JavaScript typically involves decision-making and repetition. JavaScript provides several control flow structures, including :
- conditionals (such as if, else if, and else),
- loops (such as for, while, and do-while),
- branching (such as switch).
Control flow structures enable you to control the execution of your JavaScript code based on different conditions, iterate through data, and handle errors effectively.
It's important to understand control flow in JavaScript to create logical and efficient project.
examples of JavaScript control flow constructs along with explanations :
- if statement: Executes a block of code if a specified condition is true.
- else statement: Executes a block of code if none of the preceding conditions are true.
- else if statement: Used in conjunction with if statement to specify additional conditions to be tested.
let num = 10;
if (num > 0) {
console.log("Number is positive");
} else if (num < 0) {
console.log("Number is negative");
} else {
console.log("Number is zero");
}
In this example, the if statement checks if num is greater than 0. If it is, it prints "Number is positive." If the condition is false, it moves to the else if statement to check if num is less than 0. If it is, it prints "Number is negative." If neither of the conditions is true, it executes the else block and prints "Number is zero."
Looping statements
- for loop : Repeats a block of code a specific number of times, based on a defined condition.
- while loop : Repeats a block of code while a specified condition is true.
- do-while loop : Similar to while loop, but guarantees that the block of code is executed at least once.
- for...in loop : Iterates over the enumerable properties of an object.
- for...of loop : Iterates over iterable objects, such as arrays or strings.
for (let i = 0; i < 5; i++) {
console.log(i);
}
This for loop iterates from ' 0 ' to ' 4 '. It initializes ' i ' to ' 0 ', checks if ' i ' is less than ' 5 ', executes the loop body (printing the value of ' i '), and increments ' i ' by ' 1 ' after each iteration.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Explanation: This while loop is similar to the previous example but uses a different syntax. It repeatedly executes the loop body as long as ' i ' is less than ' 5 ' .
It starts with ' i ' equal to ' 0 ' and increments ' i ' by ' 1 ' inside the loop.
Switch statement
- break statement : Terminates the current loop or switch statement and transfers control to the next statement.
- continue statement : Skips the rest of the current iteration in a loop and moves to the next iteration.
let day = "Monday";
switch (day) {
case "Monday":
console.log("It's Monday!");
break;
case "Tuesday":
console.log("It's Tuesday!");
break;
default:
console.log("It's another day.");
}
Explanation: The switch statement checks the value of the ' day ' variable. If it matches any of the cases, it executes the corresponding code block. In this example, since day is "Monday," it executes the code under the first case and prints "It's Monday!" If no cases match, it executes the code in the default block.
These examples demonstrate how control flow constructs allow you to conditionally execute code, repeat code blocks, and make decisions based on different conditions in JavaScript.