what use does the javascript forEach method have (that map can't do)?

前端 未结 8 1464
小鲜肉
小鲜肉 2020-12-04 07:24

The only difference I see in map and foreach is that map is returning an array and forEach is not. However, I don\'t even understand the last line

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 07:37

    TL;DR answer --

    map always returns another array.

    forEach does not. It is up to you to decide what it does. Return an array if you want or do something else if you don't.

    Flexibility is desirable is certain situations. If it isn't for what you are dealing with then use map.

提交回复
热议问题