---
title: "When should we use map() vs forEach() in JavaScript? - GreatStack Forum"
url: "https://greatstack.dev/forum/post/when-should-we-use-map-vs-foreach-in-javascript-PDb9W"
type: "forum_post"
author: "Dev Hacker"
date: "2026-03-29"
tags:
  - "javascript"
  - "arrays"
  - "coding"
  - "webdev"
---
# When should we use map() vs forEach() in JavaScript?

**Dev Hacker** · 2026-03-29 · Score: +2 · 💬 0 comments · 👁 35 views

Tags: `javascript`, `arrays`, `coding`, `webdev`

---

Both map() and forEach() are used to loop through arrays, but they serve different purposes.

map():

- Returns a new array

- Used when you want to transform data

forEach():

- Does not return anything

- Used for side effects like logging or updating values

Example:

const arr = [1, 2, 3];

const newArr = [arr.map](http://arr.map)(num => num * 2);

// [2, 4, 6]

arr.forEach(num => console.log(num));

So, if you need a new array → use map()

If you just want to perform an action → use forEach()

Which one do you prefer in your projects?

---

_Read and discuss at [GreatStack](https://greatstack.dev/forum/post/when-should-we-use-map-vs-foreach-in-javascript-PDb9W)._
