Facebook Pixel
Writing a Polyfill for map in JavaScript

Does map mutate the original array in JavaScript?

The answer

No. map returns a new array with the results. The original array is unchanged. A polyfill should also not mutate the original array.

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.

An optional second argument that sets this inside the callback. Support it with callback.call(thisArg, element, index, array) instead of callback(element, index, array).

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.