Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

New questions #1

Merged
merged 19 commits into from
May 4, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add questions for-each-map
  • Loading branch information
fejes713 committed May 4, 2018
commit 6501da735db6c6c4b28f5408817d9b18ba1c5656
18 changes: 18 additions & 0 deletions questions/for-each-map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### What is the difference between a `.map` loop and a `.forEach` loop

#### Answer

Both loops iterate through the elements in an array. `.map()` maps each element to new element by calling the function on each element and it returns the new array. On the other hand, `.forEach()` executes a callback function for each element but does not return anything.

#### Additional links

* [MDN docs for forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
* [MDN docs for map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
* [JavaScript — Map vs. ForEach](https://codeburst.io/javascript-map-vs-foreach-f38111822c0f)

#### Good to hear

* If you need to iterate over an array, `.forEach()` is a solid option
* If you need a result but don't want to mutate original array, `.map()` is the right choice

<!-- tags: (javascript) -->