What is the difference between forEach and map?
Answer
forEach() iterates over each element and executes a callback for its side effects — it always returns undefined and cannot be chained. Use it when you want to do something with each element (log it, send a request, update external state) without caring about the return value. map() iterates over each element and collects the return value of the callback into a new array — use it when you want to transform data. The new array is always the same length as the original. Key difference: map returns a new transformed array; forEach returns nothing. You should never use map purely for side effects (it creates an array you then discard — wasteful). You should never use forEach when you need the transformed values. Neither method modifies the original array. Both skip empty (sparse) array slots.