JS Built-ins
← Array100 / 226
ArrayArray.prototype.forEachintermediate

Array.prototype.forEach() β€” always returns undefined

πŸ“– Usage Example
const result = [1, 2, 3].forEach(n => n * 2)
console.log(result)  // β†’ undefined

forEach() always returns undefined β€” use map() instead if you need a new array.

Array.prototype.forEach() β€” return value

No matter what the callback returns, forEach always returns undefined. This distinguishes it from map, filter, and reduce.

Challenge: Implement forEachReturns(arr) that calls forEach on the array and returns its return value (which will always be undefined).

forEachReturns([1, 2, 3]) // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ `forEach` is void β€” it never returns a value.
  • β€’ Use `map` if you need to collect results from each iteration.