InterviewHubPRO
Section Overview
Q403

Question 403 of 521

Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

77%
JavaScript Internals (521 Qs)•Part 6: Async Internals, Memory, Microtasks & Web APIs (329 - 403)

What is the difference between shim and polyfill

A shim is a library that brings a new API to an older environment, using only the means of that environment. It isn't necessarily restricted to a web application. For example, es5-shim.js is used to emulate ES5 features on older browsers (mainly pre IE9). Whereas polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively. In a simple sentence, a polyfill is a shim for a browser API.

Practical Code Example

javascript
// Shim vs Polyfill:
// Polyfill: Code that replicates standard W3C/ECMAScript spec on browsers missing it (e.g., fetch polyfill).
// Shim: Broader piece of code that intercepts or provides a compatibility layer, not necessarily standard spec.