For Loop

The JavaScript for loop is used to iterate through the array or the elements for a specified number of times. If a certain amount of iteration is known, it should be used.

Syntax:

for (initialization; condition; increment)  
{
// code to be executed
}

Example: In this example, we have used for loop.

Javascript




for (let i = 1; i <= 5; i++) {
    console.log(i);
}


Output

1
2
3
4
5

Difference between forEach and for loop in Javascript

Similar Reads

For Loop

The JavaScript for loop is used to iterate through the array or the elements for a specified number of times. If a certain amount of iteration is known, it should be used....

forEach loop

...

Difference between For loop and for each loop

The forEach() method is also used to loop through arrays, but it uses a function differently than the classic “for loop”. It passes a callback function for each element of an array together with the below parameters:...

Contact Us