JavaScript for loop
In JavaScript, loops are essential when you need to perform repetitive tasks. One of the most common types of loops is the for loop
, which allows you to repeat a block of code a specified number of times. Whether you’re working with arrays, numbers, or other data structures, the for loop
is a powerful tool for iteration.
for
Loop in JavaScript?A for
loop is a control structure that allows you to repeatedly execute a block of code as long as a specified condition is true. It is commonly used for iterating over a collection (like an array) or executing a statement multiple times.
for
Loop:
for (initialization; condition; increment/decrement) {
// Code to be executed each time the loop runs
}
for
Loop ExampleThe most basic form of a for loop
is one that runs a set number of times, with the counter variable being incremented after each iteration.
for
Loop
for (let i = 0; i < 5; i++) {
console.log("Iteration number: " + i);
}
Explanation:
i = 0
and ending at i = 4
.i
is incremented by 1.
Iteration number: 0
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
for
The for
loop is often used to iterate through arrays and access each element. The loop continues until it has processed all items in the array.
let fruits = ["Apple", "Banana", "Orange", "Mango"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Explanation:
i = 0
to i = fruits.length - 1
, ensuring every element in the array is accessed.
Apple
Banana
Orange
Mango
for
Loop with a RangeYou can also use the for
loop to run a block of code a set number of times, without needing an array. This is useful when you want to loop over a range of numbers.
for (let i = 1; i <= 10; i++) {
console.log(i);
}
Explanation:
i = 1
to i = 10
, printing each number.
1
2
3
4
5
6
7
8
9
10
for
LoopA for
loop can also decrement a variable, running in reverse order. This can be useful when you need to iterate backwards through an array or range of values.
for
Loop
for (let i = 10; i >= 1; i--) {
console.log(i);
}
Explanation:
i
after each iteration until it reaches 1.
10
9
8
7
6
5
4
3
2
1
for
LoopsYou can use a for loop
inside another for loop
to handle more complex iterations, such as when working with multi-dimensional arrays or when you need to perform operations over two variables.
for
Loops
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
console.log("i = " + i + ", j = " + j);
}
}
Explanation:
i = 1, 2, 3
).i
, the inner loop also runs 3 times (for j = 1, 2, 3
).
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
for...of
Loop (ES6+)In addition to the traditional for
loop, JavaScript introduced a new loop in ES6 called the for...of
loop. This loop simplifies iteration over iterable objects such as arrays, strings, maps, and more.
for...of
to Loop Through an Array
let colors = ["Red", "Green", "Blue", "Yellow"];
for (let color of colors) {
console.log(color);
}
Explanation:
for...of
loop directly iterates over the values in the array, rather than the index.
Red
Green
Blue
Yellow
forEach()
MethodJavaScript also provides the forEach()
method for arrays, which offers a more concise and functional approach to looping through arrays.
forEach()
to Loop Through an Array
let numbers = [10, 20, 30, 40];
numbers.forEach(function(number) {
console.log(number);
});
Explanation:
forEach()
is a method available on arrays that executes a provided function once for each element in the array.
10
20
30
40
The for loop
is an essential tool in JavaScript, offering a versatile way to iterate over arrays, ranges, and more complex data structures. By understanding the different variations and techniques, such as nested loops and the for...of
loop, you can handle a wide range of tasks efficiently.
Here’s a quick summary:
for loop
: Iterate a set number of times.for loop
to access elements in an array.for loop
to iterate in reverse order.for...of
loop: An easier way to iterate over values in iterable objects (ES6+).forEach()
: A modern, functional approach to looping over arrays.By mastering the for loop
, you'll be able to handle most iterative tasks in JavaScript effectively.