JavaScript forEach Array method

  • JAVASCRIPT
  • CODING TIPS
  • ARRAY
  • FOREACH

Published on 2018-08-21

Quick Usage

For this quick usage consider that var awesomeArray = [1, 2, 3, 4, 5];.awesomeArray.forEach(function(element, index) { });Copy code to the clipboard

Details

Javascript's ES5 forEach() Array method is a fast way to iterate through all elements of an array. It executes the provided function for each element of the array, passing down the current element and index.

  • Full syntax example

    var awesomeArray = [1, 2, 3, 4, 5];
    var thisValue = window;

    awesomeArray.forEach(function(element, index, array) {
       /* do something */
    }, thisValue);
    Copy code to the clipboard
  • Paramenters

    • function(element, index, array)

      Function passing the current element, current index and the full array as paramenters.

    • thisValue

      The context on which the function is running. Defaults to undefined.

  • Return Value

    This method returns undefined.

  • Support

    All browsers.

Rafael Caferati

Full Stack Web Developer, UI/UX Javascript Specialist.

<[{ Part-time developer – full-time geek }]>

Avatar