For this example it wouldn't make any difference to be honest. Map and ForEach both iterate the array and apply the given function, their difference is that ForEach doesn't return a value just iterates the array and executes the given function on the elements but map will return a new array with the modified array.
Usually people are using map when they want to achieve immutability (they don't want to modify their array but to return a modified copy)
Example:
letnums=[1,2,3];letresult=nums.forEach(n=>n*2);// here the result variable will be undefinedletresult=nums.map(n=>n*2)// here the result variable will be [2, 4, 6]
Hi surajKurade!
For this example it wouldn't make any difference to be honest. Map and ForEach both iterate the array and apply the given function, their difference is that ForEach doesn't return a value just iterates the array and executes the given function on the elements but map will return a new array with the modified array.
Usually people are using map when they want to achieve immutability (they don't want to modify their array but to return a modified copy)
Example:
Hope this helps!
Thanks Ricky 👍