Babel

Overview

Javascript is an ever-evolving programming language which has new features added to it continuously. But not all these new features are adopted/implemented by all the browsers. New browsers or new versions of browsers provide support to it but it is not supported by all the browsers.

If you want old or older versions of browsers to understand new features, one must use a transpiler. A transpiler converts the new Javascript code to a browser compatible javascript.

That is why babel is created.

Babel is a Javascript compiler which converts the new Javascript to a browser compatible Javascript.

Check out more about babel here.

Example

ES6
let n = [1,2,3];
let squares = n.map((e) => e*e);
Transpiled to compatible Javascript
"use strict";

var n = [1, 2, 3];
var squares = n.map(function (e) {
  return e * e;
});

How to use Babel?.

In browser.

Copy the Babel cdn from here and import this in your website.

Now after importing the cdn, we need to tell the browser that the current script which we are writing is in Babel.

We can do that by setting the type of the script tag.

<script type="text/babel">
let n = [1,2,3];
let squares = n.map((e) => e*e);
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *