What is transpiling in JavaScript? (ECMAScript, babel & more)

How Old Browsers Understand Your Latest Code Versions

JavaScript is an evolving language, with new features and syntax being introduced regularly. However, not all browsers and environments support the latest JavaScript features immediately. This can create compatibility issues when developers want to use the latest language features while ensuring their code runs on all platforms.

Transpiling: Bridging the Compatibility Gap

Transpiling helps bridge this gap by transforming modern JavaScript code into older versions of JavaScript that are widely supported across different browsers.

For example, a developer might write code using the latest (ES6) features, and then use a transpiler to convert that code into (ES5) or an even older version. This way, the code remains compatible with older browsers that do not support ES6 features natively.

Babel: A Popular Transpiler

One popular transpiler for JavaScript is Babel. It allows developers to write code using the latest ECMAScript features and then transpile it into older versions of JavaScript for broader compatibility.

In addition to converting between different ECMAScript versions, transpilers can also be used for other languages that compile to JavaScript, such as TypeScript or JSX, which is a syntax extension for React.js. These languages are transpiled into plain JavaScript that can be executed in web browsers or other JavaScript environments.

Examples of Transpiling

Arrow Function Example

Consider the following arrow function introduced in ES6:


const add = (a, b) => a + b;
console.log(add(3, 5));

The transpiled ES5 version of this arrow function using Babel would look something like this:


"use strict";

var add = function add(a, b) {
  return a + b;
};

console.log(add(3, 5));

In the transpiled code, the arrow function is converted into a regular function expression using the function keyword, making it compatible with older JavaScript engines that support ES5 syntax. Note that you would need a tool like Babel and its associated presets to transpile ES6 code to ES5. You can configure Babel to target specific ECMAScript versions or environments based on your project requirements as well.

Shorthand Property Example

Here’s another simple example:


var foo = [1, 2, 3];

var obj = {
  foo // same as `foo: foo`
};

obj.foo; // [1, 2, 3]

There's an object named obj with a property foo, and just by writing foo like this within the object, it denotes that the foo property within the object is equal to the foo array declared above. This is called shorthand property, introduced in ES6, where whenever you have a variable with the same name as a property on an object, you can omit the property name.

When this is transpiled, it gets converted into something like this:


var foo = [1, 2, 3];

var obj = {
  foo: foo
};

obj.foo; // [1, 2, 3]

Instead of just writing foo, the transpiler writes the key-value pair explicitly.

Conclusion

In a nutshell, transpiling in JavaScript refers to the process of converting source code written in one version of JavaScript, or a different language that compiles to JavaScript, into another version of JavaScript that is compatible with a broader range of browsers or environments.