JS CodeLab

Array map() vs forEach() in JavaScript: Key Differences

By JS CodeLab Engineering Team•Last updated: October 12, 2026•5 min read

Direct Answer & Key Concept

The primary difference between map() and forEach() is that map() allocates and returns a new array containing transformed items, whereas forEach() executes a side effect for each element and returns undefined.

Runnable Code SnippetRun in Playground
// Comparing map and forEach
const numbers = [1, 2, 3, 4];

// Transformation: map
const squares = numbers.map(n => n * n);
console.log("Squares:", squares); // [1, 4, 9, 16]

// Side Effect: forEach
let sum = 0;
numbers.forEach(n => { sum += n; });
console.log("Sum:", sum); // 10

What is the difference between map() and forEach()?

Both Array.prototype.map() and Array.prototype.forEach() iterate through array elements from start to finish, calling a supplied callback function on each item. However, they serve fundamentally different architectural roles:

  • Use map() when your goal is to transform data and produce a new array without mutating the source data.
  • Use forEach() when your goal is to execute side effects (such as logging, updating external variables, or triggering network requests) without needing a return value.

Detailed Comparison Table: map vs forEach

| Feature | Array.prototype.map() | Array.prototype.forEach() | | :--- | :--- | :--- | | Return Value | A new array of identical length | Always undefined | | Primary Use Case | Pure data transformation | Performing external side effects | | Chainability | Yes (.map().filter().reduce()) | No (returns undefined) | | Mutates Original? | No (unless callback mutates elements) | No (unless callback mutates elements) | | Can Break Early? | No | No (use for...of or some() instead) |


Code Comparison: Practical Examples

Using map() for Pure Transformation

const rawPrices = [19.99, 4.50, 100.00];

// map returns a new array; rawPrices remains unchanged
const formattedPrices = rawPrices.map(price => `$${price.toFixed(2)}`);

console.log(formattedPrices); // ["$19.99", "$4.50", "$100.00"]

Using forEach() for Side Effects

const usersToNotify = ["Alice", "Bob", "Charlie"];

// forEach performs an action; it returns undefined
usersToNotify.forEach(user => {
  console.log(`Sending notification email to ${user}...`);
});

How to Break Early from Array Loops

Neither map() nor forEach() supports the break or continue statements. If your algorithm requires terminating early once a condition is satisfied:

  1. Use for...of loop: Supports break and continue natively.
  2. Use Array.prototype.some(): Terminates iteration as soon as a callback returns a truthy value.
  3. Use Array.prototype.find(): Stops searching immediately after locating the first match.

Common Mistakes with map and forEach

  1. Using map() without using its return value: Calling arr.map(x => console.log(x)) needlessly allocates a new array of undefined values in memory. Use forEach() instead.
  2. Expecting forEach() to return data: Writing const doubled = arr.forEach(x => x * 2) assigns undefined to doubled.
  3. Attempting to use break inside forEach: Calling break inside forEach() triggers a syntax error (SyntaxError: Illegal break statement).

Frequently Asked Questions About Array map() vs forEach() in JavaScript: Key Differences

When should I use map() instead of forEach() in JavaScript?

Use map() whenever you want to transform an array into a new array of values. Use forEach() when you want to execute side effects like logging, DOM updates, or database writes without creating a new array.

Can you break out of a forEach loop in JavaScript?

No. The forEach() method executes the callback on every element and cannot be stopped using break or continue. If you need early termination, use a standard for...of loop or Array.prototype.some().

Is map() faster than forEach()?

In modern JavaScript engines (V8, JavaScriptCore), performance differences between map and forEach are negligible for normal use cases. Choose based on semantic intent: use map for transformations and forEach for side effects.

Does forEach modify the original array?

The forEach() method itself does not mutate the original array, but the callback function you provide is capable of mutating the elements or properties of the array if written to do so.

Related Runnable JavaScript Examples

Ready to test what you learned?

The best way to solidify your JavaScript skills is by writing and running code. Open our free online compiler and test your scripts with real-time console feedback.

Open JavaScript Compiler