Polyfills in javascript

A polyfill is a piece of code that implements a modern JavaScript feature on older environments that do not support it. Polyfills usually check if a method exists and, if not, define a compatible version (for example, for Array.prototype.includes, Promise, or fetch).

Example

// Simple polyfill for Array.prototype.includes
if (!Array.prototype.includes) {
  Array.prototype.includes = function (search, fromIndex = 0) {
    for (let i = fromIndex; i < this.length; i++) {
      if (this[i] === search) return true;
    }
    return false;
  };
}

The polyfill first checks whether includes already exists; if not, it defines a compatible implementation so your code works on older browsers too.