JavaScript moves fast, and new features come. Some browsers do not support these features and cause issues for users.
Table of Content
Developers solve this with two tools, which are polyfills and transpilers. Both make old browsers handle new code so the site works everywhere.
Understand the Polyfilling in JavaScript
A polyfill is code that adds a feature when a browser does not support it. The browser acts as if it already has that feature inside.
Some browsers do not support Array.includes. A polyfill adds it and code that depends on it can run.
Users often keep old browsers for many years. A website must still work for them and not break. Polyfills give support to these browsers and keep the site usable for everyone.
What is Transpiling in JavaScript?
A transpiler takes new code and rewrites it into old syntax that old browsers can read. It does not add features but it changes the way code looks so the browser accepts it.
For example:
const sum = (a, b) => a + b;Transpiled code:
var sum = function(a, b) { return a + b; };A transpiler scans the source code and creates a new version with old syntax. The browser only sees the rewritten version and runs it without errors.
First, install Node.js and npm. Then create a project folder and set it up. After that, install Babel with npm. Add a config file for Babel in the folder. Last step is to run Babel so your code becomes older syntax.
The Difference Between Polyfilling and Transpiling
A polyfill gives a missing feature to a browser. A transpiler rewrites code into a form that works on old browsers.
Here is a table that shows you the key differences:
| Aspect | Polyfill | Transpiler |
|---|---|---|
| Purpose | Add a missing feature | Rewrite code in old syntax |
| Example | Add Array.includes | Change let into var |
| Use | Fill gap in browser | Make code run on old syntax |
Here are some examples:
- Fetch API Polyfill – Some browsers do not have fetch. A polyfill adds fetch and falls back to
XMLHttpRequest. - Promise Polyfill – Old browsers lack Promise. A polyfill creates it so async code still runs.
- Array.from Polyfill – Some browsers do not know Array.from. A polyfill adds it so array-like objects work as arrays.
You can install Babel and Core-JS with npm. Then add a Babel config that connects to Core-JS. After setup, you can run a build tool. The code now uses transpiling from Babel and polyfills from Core-JS at the same time.
Wrapping Up
You learned what a polyfill does and how a transpiler works. Also, understood how to use Babel with Core-JS in one project.
Here is a quick recap:
- A polyfill helps you add features that do not exist
- A transpiler rewrites new code into old syntax, and both make your site work on every browser.
FAQs
What is JavaScript Polyfilling and Transpiling?
- Polyfilling means adding missing features in old browsers.
- Transpiling means converting new syntax into old syntax.
// ES6 arrow function
const add = (a, b) => a + b;
// After transpiling
function add(a, b) {
return a + b;
}
Why do we need JavaScript Polyfills?
- Old browsers do not support new methods.
- Polyfills provide fallback code for compatibility.
if (!Array.prototype.includes) {
Array.prototype.includes = function(search) {
return this.indexOf(search) !== -1;
};
}
How do you use Babel for JavaScript Transpiling?
- Install Babel with npm.
- Configure babel.config.json.
- Run Babel to convert ES6 code.
// Install Babel
npm install --save-dev @babel/core @babel/cli @babel/preset-env
// babel.config.json
{
"presets": ["@babel/preset-env"]
}
// Command to transpile
npx babel src --out-dir dist
Similar Reads
The Math.pow() function in JavaScript solves one common task. It raises a number to a power. This helps avoid loops…
JavaScript added Math.cbrt to solve cube roots. Before that, developers used custom code or Math.pow with 1/3. It works with…
You use loops in JavaScript to repeat code without copying it many times. This helps when you handle lots of…
String Operators in JavaScript help you work with text. You can join, build, or change strings. Most of what you…
The Math object gives you a set of built-in functions and constants for working with numbers in JavaScript. You do…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…
The for...of loop appeared to solve the problem of complex loops over arrays and strings. It gives you a way…
You will learn what the JavaScript valueOf function does and how it works. It returns the primitive value of an…
JavaScript works with different kinds of values. Each value has a type, known as a data type. These types help…