What is a Shim in JavaScript? How to answer in an interview

⚡ Imagine You’re in a JavaScript Interview... The interviewer asks: 🧠 “Can you explain what shimming means in JavaScript — and when you’d actually use it?” Here’s how you can answer 👇 💡 What is a Shim in JavaScript? ✅ A Shim (also known as a Polyfill) is a piece of code that adds support for newer JavaScript features in older browsers or environments that don’t natively support them. ✅ It’s like giving old browsers a “compatibility upgrade” without changing their core engine. 📘 In simple words: “A shim is a fallback implementation for a feature that doesn’t exist in the runtime environment.” 🧩 Example ✅ Let’s say older browsers don’t support Array.prototype.includes(). You can shim it manually like this: if (!Array.prototype.includes) {  Array.prototype.includes = function (value) {   return this.indexOf(value) !== -1;  }; } ⚙️ Shims vs Polyfills ✅ ConceptPurposeShimAdds missing functionality by defining a method that didn’t exist before. ✅ PolyfillA more advanced shim — mimics modern API behavior to match newer ECMAScript specs. #javascript #react #interviewquestion #interviewprep #softwareengineer #frontend #developer

To view or add a comment, sign in

Explore content categories