When should you use forEach vs map in Javascript #shorts

preview_player
Показать описание
------------

Рекомендации по теме
Комментарии
Автор

Dude, you are super duper amazing. Keep it up mate

umairqamar
Автор

I was having this question for some time. And you make it so simple. although I'm just learning JavaScript.

zishanahmed
Автор

You also need to point out that forEach does not play well with async, so if you are doing async actions your best bet is to use Promise.all(arr.map to wait for all the promises, so that is one other time you should use .map

RachidBoudjelida
Автор

Hmm? Working on a project now with vanilla JavaScript. I am learning Map now. Can you use Map to pull elements from 2 separate arrays? for example if I have 1 array with fruits and a second array with veggies. Would you use Map to combine some elements from fruits and some elements from veggies using Map? Or something else? Thanks - you.

DevlogBill
Автор

Suggestion, I think you can make video about reduce. Especially in the case where they need to map then filter an array, they can simply do one loop with reduce().

Good content btw

ayubmaruf
Автор

What happens if I don’t want to return a new array, but I just want to modify the elements in place.

charlesthompson
Автор

It was asked in an interview. Q was - What is the difference between for each and map.?

maxvhanamane
Автор

I think for of loop does everything, is it safe to use for of all time or use some array methods??

naive_algorithm
Автор

There's really no reason NOT to always use map. If you ask me, forEach is redundant.

Sure you may not need to have a return value NOW. But if you do, you can always add one without having to change anything else.

Dylan_thebrand_slayer_Mulveiny
Автор

Which way better
slice -> map
map -> slice

ThanHtutZaw
Автор

Please, what's the name of the font you use in vscode (this video) ?

nasrisami
Автор

But what if I use map and discard the result? Is there any difference? Are the performance the same? If so, it would seem that map is just superior forEach and there is no reason to use forEach other than code readability (maybe?)

jack