Loops
Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions
typically return true or false. A loop will continue running until the defined condition returns
false.
Types of Loops
JavaScript provides the following types of loops:
1. for loop – Used when the number of iterations is known.
2. while loop – Repeats while a condition is true.
3. do...while loop – Executes at least once, then repeats while a condition is true.
4. for...in loop – Iterates over the properties of an object.
5. for...of loop – Iterates over iterable objects like arrays and strings.
For Loop
The for loop is used when we know how many times we need to run a block of code.
Syntax
for (initialization; condition; increment/decrement) {
// Code to execute
}
Initialization → Runs once before the loop starts.
Condition → If true, the loop runs; if false, the loop stops.
Increment/Decrement → Updates the loop variable.
Example: Print numbers from 1 to 5
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Example: Loop through an Array
, let fruits = ["Apple", "Banana", "Cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
While Loop
The while loop starts by evaluating condition. If condition evaluates to true, the code in the code
block gets executed. If condition evaluates to false, the code in the code block is not executed
and the loop ends.
Syntax
while (condition) {
// Code to execute
}
Example: Print numbers from 1 to 5
let i = 1;
while (i <= 5) {
console.log(i);
i++; // Increment
}
do...while Loop
The do...while loop is closely related to while loop. In a do...while loop, condition is checked at
the end of each iteration of the loop, rather than at the beginning before the loop runs. This
means that code in a do...while loop is guaranteed to run at least once, even if the condition
expression already evaluates to true.
Syntax
Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions
typically return true or false. A loop will continue running until the defined condition returns
false.
Types of Loops
JavaScript provides the following types of loops:
1. for loop – Used when the number of iterations is known.
2. while loop – Repeats while a condition is true.
3. do...while loop – Executes at least once, then repeats while a condition is true.
4. for...in loop – Iterates over the properties of an object.
5. for...of loop – Iterates over iterable objects like arrays and strings.
For Loop
The for loop is used when we know how many times we need to run a block of code.
Syntax
for (initialization; condition; increment/decrement) {
// Code to execute
}
Initialization → Runs once before the loop starts.
Condition → If true, the loop runs; if false, the loop stops.
Increment/Decrement → Updates the loop variable.
Example: Print numbers from 1 to 5
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Example: Loop through an Array
, let fruits = ["Apple", "Banana", "Cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
While Loop
The while loop starts by evaluating condition. If condition evaluates to true, the code in the code
block gets executed. If condition evaluates to false, the code in the code block is not executed
and the loop ends.
Syntax
while (condition) {
// Code to execute
}
Example: Print numbers from 1 to 5
let i = 1;
while (i <= 5) {
console.log(i);
i++; // Increment
}
do...while Loop
The do...while loop is closely related to while loop. In a do...while loop, condition is checked at
the end of each iteration of the loop, rather than at the beginning before the loop runs. This
means that code in a do...while loop is guaranteed to run at least once, even if the condition
expression already evaluates to true.
Syntax