How do you implement a polyfill for map?
Loop through the array, call the callback with (element, index, array), push the result to a new array, return it. Use let in the loop and do not mutate the original.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Other Polyfills to Practice for Interviews
Handle the initial value: if provided, start with it at index 0; if not, use the first element at index 1. Throw TypeError for empty arrays without initial value. Loop and update the accumulator.
Return a new Promise. Track completed count. For each promise, resolve and store the result at its index. When all complete, resolve with the results array. If any rejects, reject immediately.
Three arguments: the current element, the current index, and the array itself. Use callback.call(thisArg, this[i], i, this) to support the optional thisArg parameter.
Still have questions?
Browse all our FAQs or reach out to our support team
