What is a Polyfill?
What is a Polyfill?
Polyfill is code (usually JavaScript) that implements functionality missing in older browsers but already available in modern versions.
In other words: it's a "patch" that adds support for new API where it doesn't exist yet.
Example
Let's say you use the Array.prototype.includes method, which appeared in ES2016.
In older browsers it may not exist, and then the code will "break".
Solution — add a polyfill:
if (!Array.prototype.includes) {
Array.prototype.includes = function (item) {
return this.indexOf(item) !== -1;
};
}
Now even in an old browser your code will work
Important:
Polyfills increase bundle size — include only those you actually need!