JavaScript Array forEach() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.forEach(callback) runs your function once for every element, in order. It is for side effects (log, update UI, push into another list). It always returns undefined—use map() when you need a new array (ES5).

Remember
array.forEach(callback)

callback(element, index, array)
visits each item in order
returns undefined  (not a new array!)

return inside callback → skip rest of that call only
cannot break / continue like a for loop

Syntax

Call array.forEach. Start with this line.

JavaScript
array.forEach(callback, thisArg?)
CallWhat it does
array.forEach(callback, thisArg?)Primary signature.
array.forEach(...)Call with the arguments your case needs.

Built into JavaScript. Call forEach(...) on an array instance — no import required.

Library declarations
JavaScript
array.forEach(callback, thisArg?)

Parameters

ParameterDescription
ArrayRequired. Source to iterate
callbackRequired. Runs once per element
thisArgOptional. Optional this for the callback

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Log Each Element

Run a callback for every item in order.

JavaScript
const languages = ["JavaScript", "Python", "Java"];

languages.forEach((lang) => {
  console.log(lang);
});
Try It Yourself

How It Works

  1. Three items. Indexes run 0 … 2.
  2. Callback each time. lang is the current element.
  3. Side effect. console.log prints; nothing is collected.
  4. Done. forEach finishes and returns undefined.

Example 2 — Accumulate with an Outer Variable

Use forEach for side effects like summing into a variable.

JavaScript
const numbers = [10, 20, 30];
let total = 0;

numbers.forEach((n) => {
  total += n;
});

console.log(total);  // 60
Try It Yourself

How It Works

  1. Outer state. total lives outside the callback.
  2. Each visit. The callback adds the current n.
  3. After the loop. total holds the sum.
  4. Alternative. For pure summing, reduce() is often clearer.

Example 3 — forEach() vs map()

forEach returns undefined; map returns a new array.

JavaScript
const numbers = [1, 2, 3];

const fromForEach = numbers.forEach((n) => n * 2);
const fromMap = numbers.map((n) => n * 2);

console.log(fromForEach);  // undefined
console.log(fromMap);      // [2, 4, 6]
Try It Yourself

How It Works

  1. Same idea in the callback. Both compute n * 2.
  2. forEach discards it. The method’s return value is always undefined.
  3. map collects it. You get a new array of results.
  4. Rule. Need values → map; need actions → forEach.

Browser & Runtime Support

Array.prototype.forEach() is part of ES5 (2009). It has been available in all modern browsers for many years, including Internet Explorer 9+.

✓ Baseline · ES5

Array.prototype.forEach()

Supported in Chrome, Firefox, Safari, Edge, IE9+, and all current Node.js versions.

99% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.forEach() Excellent

Bottom line: Safe everywhere you ship modern or legacy web apps. No polyfill is needed for IE9+ or any current Node LTS release.

Frequently Asked Questions

forEach() runs a callback function once for every element in the array, in order. It is used for side effects like logging, updating the DOM, or accumulating values in outer variables.
forEach() always returns undefined. It does not produce a new array. Use map(), filter(), or reduce() when you need a transformed result.
No. break and continue do not work inside forEach(). return only skips the rest of the current callback. Use for...of or a classic for loop if you need early exit.
map() returns a new array of transformed values. forEach() returns undefined and is meant for running code on each element without building a return value.
Yes. forEach() does not call the callback for holes (empty slots) in sparse arrays.
forEach() is ES5 (2009). It works in all modern browsers and has been supported in Internet Explorer 9+.

Next: includes()

Learn how to check whether an array contains a value.

includes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful