Polyfill: map( )

Difficulty: easy

Implement Array.prototype.myMap, a version of Array.prototype.map.

For every existing item in the array, call the callback and place the returned value at the same index in a new array.

Requirements

  • Return a new array and do not mutate the original.
  • Call the callback with value, index, and the original array.
  • Throw a TypeError when callback is not a function.
  • Preserve sparse array holes like native map.

More JS Coding

View all JS Coding →
JS CodingMedium

Polyfill: call( )

JS CodingEasy

Polyfill: Array.prototype.reduce

JS CodingMedium

4. Improve a function

JS CodingMedium

5. Throttle

JS CodingMedium

13. Deep Clone

JS CodingMedium

14. Event Emitter

Related hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub

FAQ

Why implement Array.map as a polyfill in interviews?

It shows you understand callbacks, `thisArg`, sparse arrays, and returning a new array without mutating the source — core JS fluency.

What edge cases matter?

Empty arrays, holes in sparse arrays, callback exceptions, and preserving length correctly on the result.

Related polyfill practice?

Array.prototype.reduce and call polyfill, then promise utilities.