The JavaScript forEach method in detail

In this article, we are going to learn about forEach method in JavaScript in detail with examples.
Submitted by Abhishek Pathak, on October 03, 2017

JavaScript just like any other programming language supports the array data type for storing data. But unlike C/C++ JavaScript supports various methods on arrays. For this post, we will discuss about Array forEach method.

Why do we store data? To access it by a reference at later time. Generally we iterate over each element of array through for loop, which is how we do in every language. Just like that, in JavaScript we can access elements of array using for loop like,

Example:

var arr = [1,2,3,4,5];
for(var i=0; i<arr.length(); i++) {
  console.log(arr[i]);
}

Output

12345

But we have a very efficient and straight way to traverse the array, using the Array.forEach() method. Let's understand by an example.

Output

var arr = [1,2,3,4,5];
arr.forEach(function(element, index) {
  console.log('The element at index ' + index + ' is: ' + element);
}

Output

The element at index 0 is 1 the element at index 1 is 2.

Explanation

Now what's happening is the forEach method has a function as an argument, which has 3 arguments. The arguments are the element itself in each iteration; the corresponding index of the array and last one is the array itself, if the function is defined somewhere else.

If you find it useful, let us know in the comments below.

JavaScript Examples »



Related Examples



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.