JavaScript Loops
JavaScript loops are used to repeatedly execute a block of code as long as a specified condition is true. They are essential for automating repetitive tasks and iterating over collections of data. There are several types of loops in JavaScript, including for, while, and do…while. Here’s an overview of each:
In the below PDF we discuss about JavaScript Operators in detail in simple language, Hope this will help in better understanding.
Types of JavaScript Loops:
1. For Loop:
The for loop is a versatile and widely used construct in JavaScript. It allows you to execute a block of code repeatedly for a specified number of times. The basic syntax is as follows:
for (initialization; condition; iteration) {
// code to be executed
}
The initialization is executed only once at the beginning, the condition is checked before each iteration, and the iteration is performed after each iteration.
for (let i = 0; i < 5; i++) {
console.log(i);
}
2. While Loop:
The while loop is used when the number of iterations is not known beforehand, and it continues executing the block of code as long as the specified condition is true.
while (condition) {
// code to be executed
}
Be cautious with while loops to avoid infinite loops by ensuring that the condition eventually becomes false.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
3. Do-While Loop:
Similar to the while loop, the do-while loop executes the block of code at least once before checking the condition. This is useful when you want to guarantee the execution of the code inside the loop at least once.
do {
// code to be executed
} while (condition);
Examples:
let i = 0;
do {
console.log(i);
i++;
} while (i < 5)
4. For…in Loop:
The for…in loop is used to iterate over the enumerable properties of an object. It is important to note that this loop should not be used for iterating over arrays, as it may not produce the expected results.
for (let property in object) {
// code to be executed
}
Examples:
let person = {
name: 'John',
age: 30,
occupation: 'developer'
};
for (let key in person) {
console.log(key + ': ' + person[key]);
}
Related Question
A loop in JavaScript is a programming construct that allows you to repeatedly execute a block of code as long as a specified condition is true.
JavaScript has three main types of loops: for, while, and do-while.
The “for” loop consists of three parts: initialization, condition, and iteration statement. It repeats a block of code as long as the condition is true, updating the loop variable during each iteration.
The break statement is used to exit a loop prematurely. When encountered, it immediately terminates the loop and transfers control to the statement following the loop.
The continue statement is used to skip the rest of the code inside a loop for the current iteration and proceed to the next iteration.
Relevant
JavaScript JSON JSON stands for
JavaScript Function Generator Function generators,
JavaScript WeakSet A WeakSet is
JavaScript WeakMap A WeakMap is
JavaScript Proxy A JavaScript Proxy
JavaScript Boolean JavaScript Boolean is
JavaScript BigInt JavaScript BigInt represented
JavaScript Promise JavaScript Promises are
JavaScript Date Objects The JavaScript
JavaScript Set A Set in