# Map is working but forEach is not! Why?

**URL:** https://forum.kirupa.com/t/map-is-working-but-foreach-is-not-why/638385
**Category:** Uncategorized
**Created:** [June 18, 2018, 3:45pm UTC](https://forum.kirupa.com/t/map-is-working-but-foreach-is-not-why/638385 "2018-06-18T15:45:51Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![kayut](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@kayut](https://forum.kirupa.com/u/kayut)
#### Post date: [June 18, 2018, 3:45pm UTC](https://forum.kirupa.com/t/map-is-working-but-foreach-is-not-why/638385/1 "2018-06-18T15:45:51Z")

</div>

Hey,

Can some one please explain to me why this works:

```auto
let fruits = ['apple', 'orange', 'lemon'];
 
let output = fruits.map(function(item){
    return item;
});

console.log(output);
```

But the same code with forEach doesn’t work?

```auto
let fruits = ['apple', 'orange', 'lemon'];

let output = fruits.forEach(function(item){
    return item;
});

console.log(output);
```

Isn’t it that myNames is an array and forEach is a built-in method of Array?  
Why is that the forEach is not working?

Thanks

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [June 18, 2018, 4:37pm UTC](https://forum.kirupa.com/t/map-is-working-but-foreach-is-not-why/638385/2 "2018-06-18T16:37:34Z")

</div>

That’s the way forEach works. It only loops through arrays, it doesn’t also make a new array like map does.

forEach would be used if you wanted to do some work during the loop, like call a function, and nothing else. map is used for transforming data in an array to a new set of different data.

```auto
let fruits = ['apple', 'orange', 'lemon'];

let output = fruits.forEach(function(item){
    console.log(item);
    // return is ignored in forEach
});

```

> **[Array.prototype.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)**
>
> The forEach() method executes a provided function once for each array element.
