DEV Community

MurtazaBagwala07
MurtazaBagwala07

Posted on

Polyfill and shim

I will try to give a brief overview of what polyfill and shim is and why are they used.

Polyfill : It basically is a code that allows browser to support such feature which it is not capable of implementing on its own.
Generally it refers to Javascript Library which implements the HTML5 or CSS web standard.

For example, Math.floor(n) is a function that returns the largest integer less than or equal to a given number, e.g Math.floor(1.23) returns 1.

In some (very outdated) JavaScript engines, there’s no Math.floor, so such code will not work.

So how is this code supposed to work in those outdated JS engines?
Thats Where polyfill comes in.

It is a script that updates/adds new functions.It “fills in” the gap and adds missing implementations.

Shim : A shim is any piece of code that performs interception of an API call and provides a layer of abstraction. It isn't necessarily restricted to a web application or HTML5/CSS3.

The idea here is to make it normal using certain APIs across different environments. So, if two browsers implement the same API differently, you could intercept the API calls in one of those browsers and make its behavior align with the other browser.

Discussion (0)