Open links in new tab
  1. Syntax

    Parameters
    callbackFn A function to execute for each element in the array. Its return value is discarded. The function is called with the following arguments: element The current element being processed in the array. index The index of the current element being processed in the array. array The array forEach() was called upon. thisArg Optional A value to use as this when executing callbackFn. See iterative methods.

    Mozilla Developer
    Description

    The forEach() method is an iterative method. It calls a provided callbackFn function once for each element in an array in ascending-index order. Unlike map(), forEach() always returns undefined and is not chainable. The typical use case is to execute side effects at the end of a chain. Read the iterative methods section for more information about how these methods work in general.

    Mozilla Developer
    Examples

    Converting a for loop to forEach Printing the contents of an array
    The following code logs a line for each element in an array:

    Mozilla Developer
    Browser compatibility

    BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

    Mozilla Developer
  1. The forEach loop in JavaScript is a method that executes a provided function once for each array element. It is commonly used to iterate over arrays.

    Example

    const fruits = ["apple", "orange", "cherry"];
    fruits.forEach((fruit) => {
    console.log(fruit);
    });
    // Output:
    // apple
    // orange
    // cherry
    Copied!

    Description

    The forEach method calls a function for each element in an array, in ascending order. The function is called with three arguments:

    1. currentValue: The current element being processed.

    2. index: The index of the current element.

    3. array: The array forEach was called upon.

    Syntax

    array.forEach(function(currentValue, index, array), thisValue);
    Copied!

    Example with Index and Array

    const numbers = [65, 44, 12, 4];
    numbers.forEach((item, index, arr) => {
    arr[index] = item * 10;
    });
    console.log(numbers); // [650, 440, 120, 40]
    Copied!

    Important Considerations

    Feedback
  2. JavaScript Array forEach () Method - W3Schools

    Description The forEach() method calls a function for each element in an array. The forEach() method is not executed for empty elements.

  3. Loop (for each) over an array in JavaScript - Stack …

    Feb 17, 2012 · JavaScript has powerful semantics for looping through arrays and …

    • Reviews: 2
    • JavaScript Array forEach () Method - GeeksforGeeks

      Sep 2, 2024 · The JavaScript Array forEach () method is a built-in function that executes a provided function once for each array element. It does not return a new array and does not modify the original …

    • JavaScript Array forEach () Method

      In this tutorial, you will learn how to use the JavaScript Array forEach () method to execute a function on every element in an array.

    • JavaScript forEach () – JS Array For Each Loop Example

      Jun 16, 2022 · In this article, we'll look at how you can use the JavaScript forEach() array method to loop through all types of arrays, as well as how it differs from the for loop method.

    • People also ask
      Loading
      Unable to load answer
    • JavaScript | Arrays | .forEach () | Codecademy

      Jun 22, 2021 · In JavaScript, the .forEach() method loops over a given array, passing each item in the array into the callback function provided. It is a higher …

    • JavaScript's Array .forEach () Method Explained: Tips, …

      Jul 1, 2024 · The array .forEach() method is one of the most powerful tools in JavaScript, used for executing a provided function once upon an array element. …

    • Guide to JavaScript's forEach () Method - Stack Abuse

      Apr 12, 2022 · In this guide, we've taken a look at how JavaScript's forEach() method works, and how we can use it to loop through elements of an array. We've taken a look at the accompanying arguments, …

    • Javascript Array forEach () - Programiz

      In this article, you will learn about the forEach () method of Array with the help of examples.