Code has been added to clipboard!


Reading time 2 min
Published Aug 10, 2017
Updated Oct 10, 2019

JavaScript forEach: Main Tips

  • JavaScript forEach() applies a specified function for every item in a particular array individually.
  • This method only executes the specified function in an array item which has a value.
  • It's return value is undefined.
  • This loop was introduced with ECMAScript 3.
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

Usage and Purpose of forEach

The JavaScript forEach() loop is used when you want to inflict a specific method to an entire array.

Remember: arrays are lists of elements.

If you need to invoke a function to all of the array items, it's best that you apply the JavaScript array forEach loop.

The forEach() code example we have here uses a number you input to multiply every number the array contains with, and display the result in the container name demo:

<button onclick="numbers.forEach(myFunction)">Multiply</button>

Now, this example takes every value in the array, and displays the sum of those values:

var sum = 0;
var numbers = [58, 45, 122, 787];
function myFunction(item) {
    sum += item;
    sumspan.innerHTML = sum;

There are no limitations when it comes to the functions you can call on elements with the JavaScript forEach(). You can modify the items using any JavaScript function.

The specified method will not be applied to elements that have no value. For instance, the forEach JavaScript method will not affect sparse arrays in which most of the items do not have values.

The following example shows the usage of JavaScript forEach():

var numbers = [8, 2, 5];
numbers.forEach((item) => {

forEach Syntax Explained

Take a look at the code snippet below. In it, you can see the correct syntax used or writing the JavaScript forEach():

array.forEach(function(current_Value, index, array), this_Value)

The forEach JavaScript can have two main parameters: the function to be applied to the array items, and this_Value. The specified function has its arguments as well:

  • The current_value indicates the value of the currently-processed item.
  • The second parameter reveals the index of the same element.
  • The last argument is called array, and indicates the array the function is going to affect.

Note: only current_value is required to enter - the other parameters are optional.

Parameter Description
function(current_value, index, array) Needed. A function each array element to be run.
this_Value Optional. A value to be its this value.
If skipped, it will be passed as undefined.

Latest Udacity Coupon Found:



Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 05/03/2021
3176 People Used
Only 97 Left