Open In App

Difference between forEach and for loop in Javascript

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

forEach 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:

  • Current Value (required): The value of the current array element
  • Index (optional): The index number of the current element
  • Array (optional): The array object the current element belongs to

We need a callback function to loop through an array by using the forEach method.

Syntax:

numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number) {
// code to be executed
});

For every single element of the array, the function will be executed. The callback should have at least one parameter which represents the elements of an array.

Example: This example shows the use of the index parameter in the forEach method.

Javascript




numbers = [1, 2, 3, 4, 5];
 
numbers.forEach((number, index) => {
    console.log('Index: ' + index +
        ', Value: ' + number);
});


Output

Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4
Index: 4, Value: 5

Difference between For loop and for each loop

For Loop forEach Loop
It is one of the original ways of iterating over an array. It is a newer way with lesser code to iterate over an array.
It is faster in performance. It is slower than the traditional loop in performance.
The break statement can be used to come out from the loop. The break statement cannot be used because of the callback function.
The parameters are the iterator, counter, and incrementor. The parameters are the iterator, index of item, and array to iterate.
It works with the await keyword. The await keyword cannot be used due to the callback function. It may lead to incorrect output.


Similar Reads

How to loop through HTML elements without using forEach() loop in JavaScript ?
In this article, we will learn how to loop through HTML elements without using the forEach() method. This can be done in the following ways: Table of Content Using the for loopUsing the While loopUsing the 'for.....of' statementApproach 1: Using the for loopThe HTML elements can be iterated by using the regular JavaScript for loop. The number of el
3 min read
How to Loop Through an Array using a foreach Loop in PHP?
Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iterating through associative arrays where you need both t
2 min read
Difference between forEach() and map() loop in JavaScript
When we work with an array, it is an essential step to iterate on the array to access elements and perform some kind of functionality on those elements to accomplish any task. For this loops like ForEach() and Map() are used. In this article, we are going to learn the difference between the forEach() and map() loops in JavaScript. JavaScript forEac
3 min read
What is the difference between for and Foreach loop in PHP ?
Loops can be used to iterate over collection objects in PHP. The for and foreach loop can be used to iterate over the elements. for loopThe for loop works at the end of the given condition. It is used for the implementation of variables and works in a single way. The for loop does not work in the case of associative arrays. A for loop basically con
3 min read
How to Change Values in an Array when Doing foreach Loop in JavaScript ?
The forEach loop in JavaScript is one of the several ways to iterate through an array. ForEach method is different from all other looping methods as it passes a callback function for each element in the array. So, the possible approach to change the values in the array when doing foreach method in JavaScript, the method will not directly change the
3 min read
How to iterate an Array using forEach Loop in JavaScript?
Iterating through an array in JavaScript can be done in multiple ways. One of the most common methods is using the traditional for loop. In JavaScript, we have another modern approach to iterate the array which is by using the forEach method. This method is different from the traditional approach as it uses a functional approach. This approach prov
2 min read
How to use async/await with forEach loop in JavaScript ?
Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to traverse the array with the help of forEach loop.
2 min read
Determine the first and last iteration in a foreach loop in PHP?
Given an array of elements and the task is to determine first and last iteration in foreach loop. There are many ways to solve this problem which are listed below: Method 1: It is the naive method inside foreach loop to find iteration. Use a counter variable and check when the counter value is zero then it is the first iteration and when the counte
3 min read
Iterate associative array using foreach loop in PHP
Given two arrays arr1 and arr2 of size n. The task is to iterate both arrays in the foreach loop. Both arrays can combine into a single array using a foreach loop. Array: Arrays in PHP is a type of data structure that allows to storing multiple elements of similar data type under a single variable thereby saving the effort of creating a different v
2 min read
How to remove an array element in a foreach loop?
Use unset() function to remove array elements in a foreach loop. The unset() function is an inbuilt function in PHP which is used to unset a specified variable. The behavior of this function depends on different things. If the function is called from inside of any user defined function then it unsets the value associated with the variables inside i
2 min read