Babel & Transpilation
Babel is a JavaScript compiler that transforms modern syntax (ES2020+, JSX, TypeScript) into code that runs in older environments. It's the backbone of most React/Node.js build pipelines and Jest test setups.
What is Babel used for?
Babel is a compiler that converts modern JavaScript, plus syntax like JSX and TypeScript, into JavaScript that older browsers understand. It's configured with presets, such as @babel/preset-env, which reads your browser targets. Many projects now use faster tools (esbuild, SWC) for the same job, often without configuring Babel directly.
// babel.config.json
{
"presets": [
["@babel/preset-env", { "targets": "> 0.5%, not dead" }],
"@babel/preset-react"
]
}Babel Pipeline
Transpilation (syntax)
Converts new syntax into equivalent older syntax.
Polyfills (runtime APIs)
Adds missing APIs that don't exist in older environments.
Common Pitfalls
Babel in Action Demo
Code Editor
<div class="cols">
<pre>// you write (modern JS)
const greet = (user) =>
`Hi ${user?.name ?? "guest"}`;
class Timer { #ticks = 0; }</pre>
<pre>// Babel output for old browsers
var greet = function (user) {
var _ref;
return "Hi " + ((_ref = user == null
? void 0 : user.name) != null
? _ref : "guest");
};
// + helper code for the private field</pre>
</div>
<p id="out"></p>Preview
Babel: summary
Key points
- Babel transforms syntax; missing APIs (like
Promise.anyin old browsers) need polyfills such as core-js. browserslistdefines which browsers to support, so you don't over-transpile.- Next.js and Vite use SWC or esbuild by default.
Frequently asked questions
›Do I still need Babel?
Usually not directly. Modern browsers support modern JavaScript, and frameworks handle JSX and TypeScript with faster compilers. Babel remains common in older setups and for custom plugins.