InterviewHubPRO
Section Overview
Q147

Question 147 of 521

Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

28%
JavaScript Internals (521 Qs)•Part 3: DOM, Event Flow, Timers & Browser APIs (104 - 173)

What is a polyfill

A polyfill is a piece of JS code used to provide modern functionality on older browsers that do not natively support it. For example, Silverlight plugin polyfill can be used to mimic the functionality of an HTML Canvas element on Microsoft Internet Explorer 7.

There are two main polyfill libraries available,

  1. Core.js: It is a modular javascript library used for cutting-edge ECMAScript features.
  2. Polyfill.io: It provides polyfills that are required for browser needs.

Practical Code Example

javascript
// Polyfill: Custom code replicating modern browser feature on legacy engines:
if (!Array.prototype.includes) {
  Array.prototype.includes = function (searchElement) {
    return this.indexOf(searchElement) !== -1;
  };
}
console.log([1, 2, 3].includes(2)); // Works even in legacy engines!