DEV Community

Cover image for Introduction to [ES6 vs ES5 & Why,How to Transpile code] 2023
andriken sonwane
andriken sonwane

Posted on

Introduction to [ES6 vs ES5 & Why,How to Transpile code] 2023

JavaScript is an essential tool for building modern web applications, but not all web browsers support the latest version of the language, known as ECMAScript (ES). In this article, we’ll explore the differences between ES5 and ES6, and how to transpile your code from one to the other using a tool like Babel.

What is ECMAScript?

ECMAScript is a standardized version of JavaScript that aims to standardize modern JavaScript. The 5th edition, or ES5, was released in 2009 and is the most widely supported version of the language. The 6th edition, or ES6, was released in 2015 and includes many new features that make it easier to write and maintain complex JavaScript applications.

Why transpile?

One way to use the new features of ES6 is to transpile your code, which means to convert it from ES6 to ES5 using a tool like Babel. This allows you to use the new syntax and features of ES6, while still having your code run in environments that only support ES5.

Differences between ES5 and ES6

So, what are the main differences between ES5 and ES6? Here are a few key examples:

  • let and const: ES6 introduces two new ways to declare variables: let and const. let is similar to var in ES5, but has block scope rather than function scope. This means that variables declared with let are only accessible within the block of code in which they are defined. For example:
// ES5
if (true) {
  var x = 5;
}
console.log(x); // 5

// ES6
if (true) {
  let y = 5;
}
console.log(y); // ReferenceError: y is not defined

Enter fullscreen mode Exit fullscreen mode

  • const is also block scoped, but the value of a const variable cannot be reassigned. This makes it easier to create variables that should not be changed by accident. For example:
// ES6
const z = 5;
z = 10; // TypeError: Assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode

  • Arrow functions: ES6 introduces a new syntax for writing anonymous functions called arrow functions. Arrow functions are shorter and more concise than traditional function expressions, and they do not have their own this value. This can make it easier to understand the code and avoid some common pitfalls. For example:
// ES5
var add = function(a, b) {
  return a + b;
};

// ES6
const add = (a, b) => {
  return a + b;
};

Enter fullscreen mode Exit fullscreen mode

  • Classes: ES6 introduces a new syntax for creating classes and subclasses. This is a syntactic sugar on top of the prototype-based inheritance model that has been in JavaScript since the beginning. Classes make it easier to create objects with shared behavior and structure, and they can make the code more organized and easier to understand. For example:
// ES6
class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    console.log(`${this.name} makes a sound.`);
  }
}

class Dog extends Animal {
  constructor(name) {
    super(name);
  }
  speak() {
    console.log(`${this.name} barks.`);
  }
}

const dog = new Dog('Fido');
dog.speak(); // Fido barks.

Enter fullscreen mode Exit fullscreen mode

  • Template literals: ES6 introduces a new syntax for creating string templates, called template literals. Template literals can include placeholders for expressions and make it easier to create multi-line strings. This can make it easier to create strings that contain dynamic data or that span multiple lines. For example:
// ES5
var name = 'Bob';
console.log('Hello, ' + name + '!');

// ES6
const name = 'Bob';
console.log(`Hello, ${name}!`);

Enter fullscreen mode Exit fullscreen mode

  • Destructuring: ES6 introduces a new syntax for easily extracting values from arrays and objects. This can be useful for extracting values from data structures and assigning them to variables. It can make the code more concise and easier to read. For example:
// ES5
var arr = [1, 2, 3];
var a = arr[0];
var b = arr[1];
var c = arr[2];

// ES6
const [a, b, c] = [1, 2, 3];

// ES5
var obj = { a: 1, b: 2, c: 3 };
var a = obj.a;
var b = obj.b;
var c = obj.c;

// ES6
const { a, b, c } = { a: 1, b: 2, c: 3 };

Enter fullscreen mode Exit fullscreen mode

  • Modules: ES6 introduces a new syntax for organizing and sharing code between JavaScript files. This allows you to export and import variables, functions, and classes from one file to another. This can make it easier to reuse code and create more modular and maintainable applications. For example:
// In math.js
export function add(a, b) {
  return a + b;
}

// In app.js
import { add } from './math';
console.log(add(1, 2)); // 3

Enter fullscreen mode Exit fullscreen mode

How to Transpile?

To transpile your code, you will need to install Babel and configure it to work with your project. There are many ways to do this, but one common approach is to use a build tool like webpack or rollup, which can handle the transpilation process as part of their build pipeline.

Here are the basic steps for transpiling your code with Babel:

  • 1. Create a new directory for your project and navigate to it in the terminal.
project-directory

Enter fullscreen mode Exit fullscreen mode
  • 2. Initialize your project by running npm init -y. This will create a package.json file with default settings.
project-directory
└── package.json

Enter fullscreen mode Exit fullscreen mode
  • 3. Install Babel by running npm install --save-dev @babel/core @babel/cli. This will install the Babel core package and the Babel command line interface (CLI).
project-directory
├── node_modules
└── package.json

Enter fullscreen mode Exit fullscreen mode
  • 4. Configure Babel by creating a .babelrc file in the root of your project. This file should contain a JSON object that specifies the presets and plugins that you want to use with Babel. For example:
project-directory
├── .babelrc
├── node_modules
└── package.json

Enter fullscreen mode Exit fullscreen mode
  • 5. Create a src directory in the root of your project. This will be the directory where you will write your ES6 code.
project-directory
├── .babelrc
├── node_modules
├── package.json
└── src

Enter fullscreen mode Exit fullscreen mode
  • 6. Write some ES6 code in index.js. For example:
project-directory
├── .babelrc
├── node_modules
├── package.json
└── src
    └── index.js

Enter fullscreen mode Exit fullscreen mode
  • 7. Transpile your code by running babel src -d lib. This will transpile the code in the src directory and output the result to the lib directory. You can adjust the input and output directories as needed.
project-directory
├── .babelrc
├── lib
│   └── index.js
├── node_modules
├── package.json
└── src
    └── index.js

Enter fullscreen mode Exit fullscreen mode
  • 8. Create an index.html file in the root of your project. This will be the HTML file that loads your transpiled JavaScript code.
project-directory
├── .babelrc
├── index.html
├── lib
│   └── index.js
├── node_modules
├── package.json
└── src
    └── index.js

Enter fullscreen mode Exit fullscreen mode
  • 9. Include the transpiled index.js file in the index.html file using a script tag. For example:
project-directory
├── .babelrc
├── index.html
├── lib
│   └── index.js
├── node_modules
├── package.json
└── src
    └── index.js

Enter fullscreen mode Exit fullscreen mode
  • 10. Open the index.html file in a web browser to see the result of your transpiled code.

Top comments (0)