Full answer
New JavaScript features come in two kinds, and they are handled differently.
Syntax: new grammar such as arrow functions, ?., ??, classes, private fields, or async/await. An older engine cannot parse it at all, and it throws a SyntaxError for the whole file. These need transpilation: a tool rewrites them into equivalent older syntax. Babel is the classic transpiler, and SWC and esbuild do similar work faster. TypeScript's compiler also lowers syntax to a target.
APIs: new built-in functions and objects such as Array.prototype.at, Object.groupBy, Promise.allSettled, fetch, or structuredClone. Parsing works, but calling a missing function throws a TypeError at runtime. Syntax tools cannot fix this. A polyfill (from the core-js library, for example) defines the missing function if it is not already there. Some features cannot be polyfilled fully, for example Proxy.
How Babel is configured: @babel/preset-env reads your targets, either given directly or from a browserslist configuration, such as defaults, not dead or chrome >= 100. It transforms only the syntax that the targets lack. With useBuiltIns: 'usage' it adds core-js imports only for the APIs your code uses and the targets lack. Babel also adds small helper functions to the output (a class helper, a regenerator runtime for async functions on very old targets), which makes bundles bigger.
Practical points:
- Choose targets honestly. Each very old target adds code, slows builds, and increases download size for everyone. Modern teams often target the last few versions of evergreen browsers, which needs little or no transpiling.
- Serve modern code to modern browsers and a fallback bundle only if needed (
type="module"andnomodule). - A library should usually publish modern syntax and let the application's build step lower it for the application's own targets.
- Source maps map the transformed code back to the original for debugging.
- Check support with a compatibility table instead of guessing.
const babel = require('@babel/core');
function transform(code, targets) {
return babel.transformSync(code, {
babelrc: false,
configFile: false,
presets: [['@babel/preset-env', { targets }]],
}).code;
}
const cases = {
'arrow function': 'const double = (n) => n * 2;',
'template literal': 'const text = `Hello ${name}`;',
'optional chaining and ??': 'const city = user.address?.city ?? "unknown";',
'new API call, not syntax': 'const last = list.at(-1);',
};
for (const [label, code] of Object.entries(cases)) {
console.log(`--- ${label}`);
console.log('modern target (chrome 120):', transform(code, { chrome: '120' }).replace(/\n/g, ' '));
console.log('old target (chrome 40) :', transform(code, { chrome: '40' }).replace(/\n/g, ' '));
}
console.log('--- a missing API fails only when it is called');
try {
const missing = {};
missing.at(0);
} catch (error) {
console.log('missing method gives:', error.name);
}
console.log('@babel/core version:', babel.version);Run with Node.js v24.22.1 as js-145.js; two runs gave identical output. Output: --- arrow function modern target (chrome 120): const double = n => n * 2; old target (chrome 40) : var double = function double(n) { return n * 2; }; --- template literal modern target (chrome 120): const text = `Hello ${name}`; old target (chrome 40) : var text = "Hello ".concat(name); --- optional chaining and ?? modern target (chrome 120): const city = user.address?.city ?? "unknown"; old target (chrome 40) : var _user$address$city, _user$address; var city = (_user$address$city = (_user$address = user.address) === null || _user$address === void 0 ? void 0 : _user$address.city) !== null && _user$address$city !== void 0 ? _user$address$city : "unknown"; --- new API call, not syntax modern target (chrome 120): const last = list.at(-1); old target (chrome 40) : var last = list.at(-1); --- a missing API fails only when it is called missing method gives: TypeError @babel/core version: 8.1.5
Key points
- Transpilation rewrites unsupported syntax; polyfills add missing runtime APIs; they solve different problems.
- Old engines fail on new syntax with a SyntaxError for the whole file; missing APIs fail with a TypeError when called.
- preset-env uses targets (browserslist) so it only transforms what is needed; useBuiltIns usage adds needed polyfills.
- Smaller target lists mean smaller and faster bundles; helpers and polyfills add size.
- Use source maps, and let applications, not libraries, decide the final syntax level.
Common mistakes
- Thinking Babel adds missing APIs by itself without a polyfill setup.
- Targeting very old browsers by default and shipping large bundles to everyone.
- Publishing a library transpiled to very old syntax.
- Assuming that a polyfill can reproduce every feature, such as Proxy.
Follow-ups
- What is browserslist and how do build tools share it?
- How do the module and nomodule script attributes serve different bundles?
- Why are SWC and esbuild often faster than Babel, and what do they give up?
Tips
- Separate syntax from APIs in the first sentence.
- Mention targets and browserslist as the lever for bundle size.
- Say that a library should leave final lowering to the application.