Why do we use polyfills?

JavaScriptPayPal

Why do we use polyfills?

A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.

Imagine you're trying to use a new, cool feature in your website code, but some older browsers don't understand it yet. This is where polyfills come in - they're pieces of code that provide a fallback solution for your code to work on older browsers.

Here's a real world example:

  • Let's say you want to use a modern JavaScript method like Array.includes().
  • Some older browsers don't know what this is.
  • A polyfill would add this missing feature to those older browsers by creating a similar function that works the same way.

Writing polyfills is a common coding interview question. Interviewers often ask candidates to implement polyfills for built-in JavaScript methods like Array.prototype.map(), Array.prototype.filter(), Array.prototype.reduce(), Array.prototype.bind() or Promise.all().

Resources

00:00