GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Introduction3 min read
  • Variables5 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Array forEach() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Array forEach() Method

Last Updated : 1 Jun, 2026

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 array. It's commonly used for iteration and performing actions on each array element.

Syntax

array.forEach(callback(element, index, arr), thisArg);

Parameters

This method accepts five parameters as mentioned above and described below:

ParameterDescription
callbackIt is a callback function executes for each array element.
elementThe current element being processed in the array.
index (Optional)The index of current element. The array indexing starts from 0.
array (Optional)The array on which forEach() is called.
thisArg (Optional)Value to use as this when executing the callback function.

Return value

This method may or may not change the original array provided as it depends upon the functionality of the argument function.

Example 1: Basic iteration to print array elements on console.

JavaScript
const arr = [1, 2, 3, 4, 5];

arr.forEach((item) => {
    console.log(item);
});

Example 2: Copy elements from one array to another using forEach().

JavaScript
// JavaScript Array forEach() Method

// Original array
const items = [12, 24, 36];
const copy = [];

items.forEach(function (item) {
    copy.push(item);
});

console.log(copy);

Example 3: Calculates the square of every element of the array using forEach() method.

JavaScript
// JavaScript Array forEach() Method

// Original array
const items = [1, 29, 47];
const squareOfItems = [];

items.forEach(function (item) {
    squareOfItems.push(item * item);
});

console.log(squareOfItems);

Limitations of forEach() Method

  • No Break or Continue: Unlike for loops, you cannot break the forEach() loop or use continue to skip to the next iteration. It will always iterate over all elements.
  • No Return Value: The forEach() loop does not return a new array, it returns undefined.
  • Asynchronous Issues: The forEach() loop does not handle asynchronous operations well. If you need to perform asynchronous operations, consider using for...of with async/await or Promise.all.
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.