Skip to content

Creating Polyfills

A polyfill is code that adds a standard feature to an environment that lacks it, so that the same script works in older browsers and in new ones. It checks whether the feature exists, and only if it does not, it defines its own version that behaves the same way. Newer engines keep their built-in version, which is usually faster.

Polyfills matter when a site has to support browsers older than the features it uses. For example, Array.prototype.at() reached the main browsers in 2021 and 2022, and Object.groupBy() in 2023 and 2024, so a page that uses them and must still run on an older browser needs a polyfill for each.

A polyfill follows three steps:

  1. Test for the feature, so that the built-in version is never replaced.
  2. Define it on the same object as the standard version, which for an array method is Array.prototype.
  3. Match the standard’s behaviour, including what it does with unusual arguments.

This example adds at(), which returns the item at an index and counts from the end when the index is negative. Node.js already has at(), so the first line deletes it, which imitates an older engine.

// Simulate an older engine that lacks Array.prototype.at
delete Array.prototype.at;
if (!Array.prototype.at) {
Object.defineProperty(Array.prototype, 'at', {
value: function at(index) {
const position = Math.trunc(index) || 0;
const actual = position < 0 ? this.length + position : position;
return actual >= 0 && actual < this.length ? this[actual] : undefined;
},
writable: true,
configurable: true,
enumerable: false,
});
}
const temperatures = [12, 15, 9, 18];
console.log(temperatures.at(0), temperatures.at(-1), temperatures.at(10));

This prints:

12 18 undefined

The method uses a regular function rather than an arrow function, because it needs this to be the array it was called on. Math.trunc(index) || 0 turns a fraction into a whole number and treats a missing or invalid index as 0, which is what the standard specifies.

Built-in methods are not enumerable, so they do not appear when a for...in loop lists an object’s properties. A method added by plain assignment is enumerable, so it shows up in every such loop over every array.

delete Array.prototype.at;
Array.prototype.at = function (index) {
return this[index < 0 ? this.length + index : index];
};
const fruit = ['apple', 'pear'];
for (const key in fruit) {
console.log(key);
}

This prints:

0
1
at

Object.defineProperty() with enumerable: false avoids this, and writable and configurable set to true match the way the built-in methods are defined. Property Descriptors explains these settings.

A polyfill should add a feature that the standard already defines, so that it can be removed once every supported browser has it. Adding methods of your own invention to built-in prototypes, such as an Array.prototype.onlyStrings, causes problems:

  • A future version of JavaScript may add a method with the same name and different behaviour. Exactly this happened when an old library’s Array.prototype.flatten stopped the standard from using that name, so the standard method is called flat() instead.
  • Two libraries on one page may each add a method with the same name, and the one that loads last silently wins.

For a helper of your own, write an ordinary function, such as onlyStrings(array), or put it in a module.

Most projects do not write polyfills by hand. A library such as core-js supplies tested polyfills for the whole standard library, and build tools can include only the ones the target browsers need. Writing one is still the best way to understand exactly what a built-in method does.

A polyfill can add only functions and objects. New syntax, such as optional chaining (?.) or class fields, cannot be polyfilled, because an older engine fails to parse the script before any polyfill can run. Syntax needs a compiler such as Babel, which rewrites the code into an older form. Before adding either, check whether the target browsers already support the feature, which MDN’s compatibility tables and Can I use show.

Steve Griffith covers this topic in his video Creating JavaScript Polyfills.