What are common mistakes when writing a map polyfill in an interview?
Using var in the loop (closure bug with async callbacks), mutating the original array, not passing all three arguments to the callback, and using forEach to build the result (forEach returns undefined).
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Writing a Polyfill for map in JavaScript
Add a method to Array.prototype that loops through the array, calls the callback with (element, index, array), pushes the result to a new array, and returns the new array. Use let in the loop and do not mutate the original.
Three arguments: the current element, the current index, and the array itself. Most callbacks only use the first one, but the polyfill should pass all three to match the spec.
No. map returns a new array with the results. The original array is unchanged. A polyfill should also not mutate the original array.
Still have questions?
Browse all our FAQs or reach out to our support team
