Control flow refers to the order of the statements execution in a JavaScript program. It allows to make decisions, repeat actions, and control the flow of your code based on certain conditions.
There are 2 Type Control Flow in JavaScript.
It is used to execute different code blocks based on specific conditions. There are 4 type of conditional statements.
It executes a block of code only if a condition is true.
Syntax:
if (condition) {
// Code to be executed if condition is true
}
let age = 25;
if (age >= 18) {
console.log("You are an adult.");
}
It executes a block of code if the condition is true, otherwise it will execute an another block of code.
Syntax:
if (condition1) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
let age = 25;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor");
}
It executes a block of code if the condition is true otherwise it executes another block of code when another condition is true. It also called as nested if statements.
Syntax:
if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition1 is false and condition2 is true
}
let score = 85;
if (score >= 90) {
console.log("Excellent");
} else if (score >= 80) {
console.log("Good");
} else {
console.log("Needs improvement");
}
It executes different code blocks based on the value of an expression.
Break statement is necessary to stop at specific case condition. Otherwise it will execute further statments as well.
Syntax:
switch (expression) {
case value1:
// Code to be executed if expression is equal to value1
break;
case value2:
// Code to be executed if expression is equal to value2
break;
default:
// Code to be executed if no case matches
break;
}
let day = "Sunday";
switch (day) {
case "Saturday":
console.log("It's weekend!");
break;
case "Sunday":
console.log("It's weekend!");
break;
default:
console.log("It's working day!");
}
Loops are used to repeatedly execute a block of code until a certain condition is met.
A Loop has 3 important states which are used to determine the number of times to execute.
- Iteration Initialization
- Condition Check
- Increment/Decrement of Iteration
It executes a block of code a specified number of times.
For loop has iteration initialization, condition check, increment/decrement of iteration are inline.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
It iterates over the properties of an object.
Syntax:
for (let property in object) {
// Code to be executed
}
let person = { name: "kumar", age: 30 };
for (let key in person) {
console.log(key + ": " + person[key]);
}
It iterates over the values of an iterable object such as arrays, strings.
Syntax:
for (let value of iterable) {
// Code to be executed
}
let fruits = ["apple", "banana", "orange"];
for (let fruit of fruits) {
console.log(fruit);
}
It executes a block of code as long as a condition is true. This kind of loop is useful for the condition is decided based on the block of code in this loop.
Syntax:
while (condition) {
// Code to be executed
// condition is decided based on the calculation or process or execution
}
let number = 5;
var factorial = 1;
while (number > 0) {
factorial *= number;
number--;
}
console.log(factorial);
It executes a block of code at least once, then repeats as long as a condition is true.
Syntax:
do {
// Code to be executed
} while (condition);
let number = 5;
var factorial = 1;
do {
factorial *= number;
number--;
}while (number > 0);
console.log(factorial);
- Conditional Statements:
- These statements are used to decide the flow of code or execution.
- Loops:
- These loops are used to execute certain process or code or statements again and again. To reduce repeated lines of code.