map vs forEach | Javascript Most asked Interview Questions

preview_player
Показать описание
If your answer is map() returns a new array with the results of the operation performed on each element, while forEach() simply performs the operation on each element without returning anything. Additionally, map() does not modify the original array while forEach() can modify the original array.
map() method can also modify the original array
However, it's important to note that modifying the original array with map() is not a recommended practice as it can lead to unexpected behavior and make the code harder to understand and maintain. It's better to use map() to create a new array with the desired changes and leave the original array unchanged.

So our answer should be , map() returns a new array with the results of the operation performed on each element, while forEach() simply performs the operation on each element without returning anything.
Don’t say map() does not modify the original array while forEach() can modify the original array.

#frontend #frontenddeveloper #interview #javascript #map #foreach
Рекомендации по теме
Комментарии
Автор

Simply that both methods are looping through the array then you directly modify the element meaning that you modify the array

longminhdone
Автор

Foreachother if we need a modification in data, then that
Method needed to specify function inside what is the address location needed modification, but you see map return data will store that array index

sabaris
Автор

thanks for sharing, but Iam having a hard time understanding why u added x, I, a in the arrow function.

blal
Автор

hey. I appreciate your teaching way. may I know which software you are using for recording?. I am a reactjs developer 🙂

laibaiqbal
Автор

The real difference is that forEach doesn't return anything whereas map returns a new array, as I understand it

matthewbarberdev