Array.prototype.forEachintermediateconst nums = [1, 2, 3]
// forEach β side effects only
nums.forEach(n => console.log(n))
// map β returns new array
nums.map(n => n * 2) // β [2, 4, 6]Choose forEach() for side effects and map() when you need a transformed array.
You can implement map manually using forEach by pushing each mapped result into an external array. This illustrates the difference: map builds a new array automatically; forEach requires you to do it manually.
Challenge: Implement mapWithForEach(arr, fn) that applies fn to each element and returns a new array β using forEach instead of map.
mapWithForEach([1, 2, 3], n => n * 2) // β [2, 4, 6]