What is Babel? · Babel (2024)

Babel is a JavaScript compiler

Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments. Here are the main things Babel can do for you:

  • Transform syntax
  • Polyfill features that are missing in your target environment (through a third-party polyfill such as core-js)
  • Source code transformations (codemods)
  • And more! (check out these videos for inspiration)

JavaScript

// Babel Input: ES2015 arrow function
[1, 2, 3].map(n => n + 1);

// Babel Output: ES5 equivalent
[1, 2, 3].map(function(n) {
return n + 1;
});

tip

For an awesome tutorial on compilers, check out the-super-tiny-compiler, which also explains how Babel itself works on a high level.

ES2015 and beyond

Babel has support for the latest version of JavaScript through syntax transformers.

These plugins allow you to use new syntax, right now without waiting for browser support. Check out our usage guide to get started.

JSX and React

Babel can convert JSX syntax! Check out our React preset to get started. Use it together with the babel-sublime package to bring syntax highlighting to a whole new level.

You can install this preset with

and add @babel/preset-react to your Babel configuration.

JSX

export default function DiceRoll(){
const getRandomNumber = () => {
return Math.ceil(Math.random() * 6);
};

const [num, setNum] = useState(getRandomNumber());

const handleClick = () => {
const newNum = getRandomNumber();
setNum(newNum);
};

return (
<div>
Your dice roll: {num}.
<button onClick={handleClick}>Click to get a new number</button>
</div>
);
};

tip

Learn more about JSX

Type Annotations (Flow and TypeScript)

Babel can strip out type annotations! Check out either our Flow preset or TypeScript preset to get started. Keep in mind that Babel doesn't do type checking; you'll still have to install and use Flow or TypeScript to check types.

You can install the flow preset with

  • npm
  • Yarn
  • pnpm
npm install --save-dev @babel/preset-flow

JavaScript

// @flow
function square(n: number): number {
return n * n;
}

or the typescript preset with

  • npm
  • Yarn
  • pnpm
npm install --save-dev @babel/preset-typescript

JavaScript

function Greeter(greeting: string) {
this.greeting = greeting;
}

tip

Learn more about Flow and TypeScript!

Pluggable

Babel is built out of plugins. Compose your own transformation pipeline using existing plugins or write your own. Easily use a set of plugins by using or creating a preset. Learn more →

Create a plugin on the fly with astexplorer.net or use generator-babel-plugin to generate a plugin template.

example-babel-plugin.js

// A plugin is just a function
export default function({ types: t }) {
return {
visitor: {
Identifier(path) {
let name = path.node.name; // reverse the name: JavaScript -> tpircSavaJ
path.node.name = [...name]
.reverse()
.join("");
},
},
};
}

Debuggable

Source map support so you can debug your compiled code with ease.

Spec Compliant

Babel tries to stay true to the ECMAScript standard, as much as reasonably possible. It may also have specific options to be more spec compliant as a tradeoff to performance.

Compact

Babel tries using the least amount of code possible with no dependence on a bulky runtime.

This may be difficult to do in cases, and there are "assumptions" options that tradeoff spec compliancy for readability, file size, and speed.

What is Babel? · Babel (2024)
Top Articles
Best Genealogy Websites For Different Research Scenarios - Legacy Tree
How Much Do Product Designers Make? (2024 Salary Guide) | Untitled UI
Dte Outage Map Woodhaven
Fat People Falling Gif
Wisconsin Women's Volleyball Team Leaked Pictures
50 Meowbahh Fun Facts: Net Worth, Age, Birthday, Face Reveal, YouTube Earnings, Girlfriend, Doxxed, Discord, Fanart, TikTok, Instagram, Etc
Florida (FL) Powerball - Winning Numbers & Results
Brenna Percy Reddit
2016 Hyundai Sonata Price, Value, Depreciation & Reviews | Kelley Blue Book
Nioh 2: Divine Gear [Hands-on Experience]
735 Reeds Avenue 737 & 739 Reeds Ave., Red Bluff, CA 96080 - MLS# 20240686 | CENTURY 21
Diesel Mechanic Jobs Near Me Hiring
Nene25 Sports
Find Such That The Following Matrix Is Singular.
Star Wars: Héros de la Galaxie - le guide des meilleurs personnages en 2024 - Le Blog Allo Paradise
Puretalkusa.com/Amac
Best Uf Sororities
Missed Connections Dayton Ohio
Ge-Tracker Bond
Cvs El Salido
Espn Horse Racing Results
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Kabob-House-Spokane Photos
Craigslist Hunting Land For Lease In Ga
Jayme's Upscale Resale Abilene Photos
2011 Hyundai Sonata 2 4 Serpentine Belt Diagram
Rugged Gentleman Barber Shop Martinsburg Wv
Harbor Freight Tax Exempt Portal
Paradise Point Animal Hospital With Veterinarians On-The-Go
Franklin Villafuerte Osorio
Boneyard Barbers
Melissa N. Comics
Jambus - Definition, Beispiele, Merkmale, Wirkung
Workboy Kennel
Forager How-to Get Archaeology Items - Dino Egg, Anchor, Fossil, Frozen Relic, Frozen Squid, Kapala, Lava Eel, and More!
Www Violationinfo Com Login New Orleans
Uhaul Park Merced
Kips Sunshine Kwik Lube
Atlantic Broadband Email Login Pronto
The Boogeyman Showtimes Near Surf Cinemas
Letter of Credit: What It Is, Examples, and How One Is Used
Vons Credit Union Routing Number
Locate phone number
Executive Lounge - Alle Informationen zu der Lounge | reisetopia Basics
Sallisaw Bin Store
Valls family wants to build a hotel near Versailles Restaurant
Trending mods at Kenshi Nexus
Costco The Dalles Or
Scott Surratt Salary
Vrca File Converter
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 5789

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.