A loop is a programming way to run a piece of code repeatedly until a certain condition is met.
Loop Type | Definition | Syntax | Example |
---|---|---|---|
for |
Executes a block of code a specified number of times. | for (initialization; condition; increment/decrement) { // code to be executed } |
for (let i = 0; i < 5; i++) { console.log(i); } |
while |
Executes a block of code as long as a specified condition is true. | while (condition) { // code to be executed } |
let x = 0; while (x < 5) { console.log(x); x++; } |
do-while |
Executes a block of code at least once, then repeats as long as a specified condition is true. | do { // code to be executed } while (condition); |
let x = 0; do { console.log(x); x++; } while (x < 5); |
for...of |
Iterates over the elements of an iterable object (e.g., arrays, strings). | for (let element of iterable) { // code to be executed } |
let fruits = ["apple", "banana", "cherry"]; for (let fruit of fruits) { console.log(fruit); } |
for...in |
Iterates over the properties of an object. | for (let property in object) { // code to be executed } |
let person = { name: "Alice", age: 30 }; for (let prop in person) { console.log(prop + ": " + person[prop]); } |
- Iterates a block of code a specific number of times.
- Better for conditions with initialization and increment because all can be set in one line of code.
//for loop
for (let i=0; i<5; i++){
console.log(i);
}
//Output: 0 1 2 3 4
- Executes a block of code while a certain condition is true.
- Better when there is only a condition, no initialization, and no increment.
//while loop
let j = 0
while (j<5) {
console.log(j);
j++
}
//Output: 0 1 2 3 4
//condition only
while ("a"=="a"){
console.log("Happy")
}
//Output: Happy (infinity)
- Executes a block of code while a certain condition is true.
// while loop
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
// Output: 0 1 2 3 4
- Similar to the while loop, except that the block of code is
executed at least once
, even if the condition is false.
// do-while loop
let k = 0;
do {
console.log(k);
k++;
} while (k > 1);
// Output: 0
- Terminates the loop.
// break statement
for (let i = 1; i <= 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}
// Output: 1 2
Skips the current iteration
of the loop and moves on to the next iteration.
// continue statement
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
// Output: 1 2 4 5
- Slightly more complex with more lines of code.
let arr = [1, 2, 3];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// Output: 1 2 3
Much simpler
and better for iterating arrays.
let arr = [1, 2, 3];
for (let val of arr) {
console.log(val);
}
// Output: 1 2 3
- Used to loop through the values of an object like arrays, strings.
- Allows you to access each value directly, without having to use an index.
let arr = [1, 2, 3];
for (let val of arr) {
console.log(val);
}
// Output: 1 2 3
- Used to loop through the properties of an object.
- Allows you to iterate over the keys of an object and access the values associated by using keys as the index.
const person = {
name: 'Happy',
role: 'Developer'
};
for (let key in person) {
console.log(person[key]);
}
// Output: Happy Developer
- Available on arrays or objects.
- Iterates over each element of the array or object and performs some action on each element.
const array = [1, 2, 3];
//for...of loop
for (let item of array) {
console.log(item);
}
// Output: 1 2 3
//forEach method
array.forEach(function(item) {
console.log(item);
});
// Output: 1 2 3
const person = {
name: 'Happy',
role: 'Developer'
};
//for-in loop
for (let key in person) {
console.log(person[key]);
}
// Output: Happy Developer
//forEach method
Object.values(person).forEach(value =>{
console.log(value);
})
//Output: Happy Developer
forEach
is a method specific to arrays and objects, whilefor...of
andfor...in
are more general loop constructs.for...of
iterates over values, whilefor...in
iterates over keys.forEach
doesn't provide a way to break out of the loop early, whilefor...of
andfor...in
do.
-
The for...of loop is suitable when you need
more control over the loop
, such as using the break or continue statement inside. -
The forEach method
iterates over each element
of the array and performs some action on each element.
const array = [1, 2, 3];
// for-of loop
for (let item of array) {
console.log(item);
if (item === 2) {
break; // Exit the loop when item is 2
}
}
// Output: 1 2
// forEach method
array.forEach(function(item) {
console.log(item);
if (item === 2) {
break; // Error: Illegal break statement
}
});