TEDSF Interview Skills Q&A Platform
1 like 0 dislike
6 views
Can you describe the main difference between the Array.forEach() loop and Array.map() methods and why you would pick one versus the other?
in Web Development by Platinum (104k points) | 6 views

1 Answer

0 like 0 dislike
Best answer

JavaScript can be confusing sometimes. You may have come across the two popular methods Array.Prototype.map() and Array.Prototype.forEach() in your codebase. If you are unsure about their differences, keep reading. This also happens to be a popular frontend developer interview question.

Array.prototype.forEach

Anytime you want to iterate through the items in an array, the first thing that comes to our mind in any programming language is the for loop. forEach() in JavaScript is an interesting alternative to the regular for loops.

The forEach() iterates through the elements in the array. It calls a provided callback function once for each element in the array in ascending order.

The callback function accepts three arguments:

  • value of the element
  • index of the element
  • array object

Let’s take a look at an example to see how forEach() works.

let temp = [1, 4, 9];
temp.forEach((item, index) => {
 return temp[index] = item * 3;
});

// temp is now [3, 12, 27]

Array.prototype.map

The map() method creates a new array with the results of calling a provided function on every element in the calling array. map() calls a provided callback function once for each element in an array, in order, and constructs a new array from the results.

The callback accepts three arguments:

  • value of the element
  • index of the element
  • array object

You may have used the map() function before. It qualifies as a higher-order function, because it takes in a callback function as an input argument.

let numbers = [1, 4, 9];
let triples = numbers.map((item) => {
 return item * 3;
});

// numbers is still [1, 4, 9]
// triples is [3, 12, 27]

In the example above, we have an array of numbers and creating a new array using the map(). The map() takes a function as an argument. The argument item within the function will automatically be assigned from each element of the array as map() loops through the original array.

What is the difference?

If you notice closely, although they look very similar there is a fundamental difference between forEach() and map() functions.

forEach() changes the original array, whereas map() returns a new array, without mutating the original array.

by Platinum (104k points)

Related questions

1 like 0 dislike
1 answer
1 like 0 dislike
1 answer
0 like 0 dislike
0 answers
asked Oct 17, 2019 in Automotive by TEDSF Platinum (104k points) | 4 views
0 like 0 dislike
0 answers
0 like 0 dislike
0 answers
asked Aug 8, 2019 in Computer Science by TEDSF Platinum (104k points) | 6 views
0 like 0 dislike
0 answers
Welcome to TEDSF Skills Questions and Answers, a platform, where you can ask skills questions and receive answers from other members of the community. On TEDSF the youth, students, teachers, policy makers and enthusiasts can ask and answer any questions. Get help and answers to any skills-related problem including mathematics, computer science, data science, web development, physics, chemistry, digital marketing, African development and more. Help is always 100% free!

4.1k questions

1.4k answers

64 comments

29.6k users

4,058 questions
1,448 answers
64 comments
29,583 users