Getting Started with ES6 and Modern JavaScript

Learn what ES6 is, how it differs from ES5, why it matters, and how to run it in browsers, transpile it with Babel, and practice online.

🎯 Introduction

This article introduces ES6, also known as ECMAScript 2015, and its key differences from ES5. You will learn why ES6 is an essential skill for modern frontend development and how to run ES6 code directly in a browser. We will also cover how Babel helps support older browsers and introduce online environments for practicing and testing ES6 code.


🌟 What Is ES6?

ES6 is a major update to JavaScript, officially called ECMAScript 2015. Released in 2015, it introduced features that make JavaScript more concise, more capable, and better suited to building complex applications.

Think of ES6 as an upgrade package for JavaScript. It adds language features such as let and const declarations, arrow functions, classes, and modules. Learning ES6 helps you write modern code more efficiently.

Imagine ES5 as an older phone operating system and ES6 as a redesigned smartphone system with more features and easier controls.

🔄 ES5 and ES6 Compared

ES5 was released in 2009 and is widely used in older projects and browsers. ES6 followed in 2015, introducing modern programming patterns and syntactic conveniences.

Here are a few examples:

  1. Variable declarations
    ES5 uses var, whose hoisting and scoping behavior can be confusing.
    ES6 introduces let and const with block scope.
// ES5
var name = 'Tom';
if (true) {
    var name = 'Jerry';
    console.log(name); // Jerry
}
console.log(name); // Jerry; the variable was overwritten

// ES6
let name = 'Tom';
if (true) {
    let name = 'Jerry';
    console.log(name); // Jerry
}
console.log(name); // Tom; block scope preserves the outer variable
  1. Function syntax
    ES6 arrow functions are more concise and inherit this from their enclosing scope, avoiding common pitfalls.
// ES5
var sum = function (a, b) {
    return a + b;
};

// ES6
const sum = (a, b) => a + b;
  1. Classes and modules
    ES5 uses constructor functions to simulate classes, which requires more boilerplate.
    ES6 supports the class keyword for a more direct syntax.
// ES5
function Person(name) {
    this.name = name;
}
Person.prototype.sayHi = function () {
    console.log('Hi, ' + this.name);
};

// ES6
class Person {
    constructor(name) {
        this.name = name;
    }
    sayHi() {
        console.log(`Hi, ${this.name}`);
    }
}

These are just a few examples. ES6 also includes template literals, destructuring assignment, default parameters, spread syntax, and more, all of which streamline development.


🎯 Why Learn ES6?

Learning ES6 is about more than writing cleaner syntax:

  • Broad browser support
    Modern browsers such as Chrome, Firefox, Edge, and Safari support ES6 natively, and most new projects use it.
  • Frameworks and libraries use ES6
    Examples and recommended patterns for popular frameworks such as Vue, React, and Angular use ES6.
  • More efficient development and better code quality
    ES6 provides concise syntax and powerful features that reduce repetitive code.
  • A foundation for your career
    ES6 is a fundamental skill for frontend developers.
Think of ES6 as a new engine for JavaScript that helps you build more capable, reliable code.

💻 Using ES6 in the Browser

Mainstream browsers support most ES6 features. You can try arrow functions, let, const, template literals, and other features directly in the developer tools console.

Older browsers, such as IE11, lack support for many ES6 features. If you need to support them, use a transpiler.

Open your browser console and try this example:

let name = 'Alice';
const greet = () => console.log(`Hello, ${name}!`);
greet();

🔧 Introducing Babel

Babel is a widely used JavaScript transpiler. It converts ES6 syntax into ES5 syntax to help your code run in older browsers.

In other words, Babel acts as a translator.

How Do You Use Babel?

Projects typically use Babel CLI or integrate Babel with build tools such as Webpack or Rollup to transpile code automatically.

You can also paste ES6 code into Babel's online compiler and inspect the generated ES5 code, which is useful for learning and debugging.

Babel does not automatically solve every compatibility issue. New APIs may also require polyfills.

🌐 Online Coding Environments

The following online editors and playgrounds are useful for practicing ES6:

  • JSFiddle
    An established frontend editor supporting HTML, CSS, and JavaScript for quick experiments. URL: https://jsfiddle.net/
  • CodeSandbox
    A full-featured environment with project management and module imports that resembles a local development setup, suitable for React and Vue projects. URL: https://codesandbox.io/
  • JS Bin
    A simple editor with live previews for trying small code snippets. URL: https://jsbin.com/

❓ Knowledge Check

Q1: What are the main differences between ES6 and ES5? ES6 introduces block-scoped let and const declarations, arrow functions, classes, modules, and other features that make the language more concise and capable.

Q2: Why use Babel? Some browsers lack ES6 support. Babel converts ES6 syntax into backward-compatible ES5 syntax.


🧪 Exercises

Exercise 1: Declare variables with let and const, then write an arrow function that logs a template literal.

// Write your code here

Exercise 2: Create an ES6 class named Person with a name property and a sayHello method. Create an instance and call the method.

// Write your code here

🎉 You now understand the basic concepts, main features, and usage of ES6! Keep practicing to explore modern JavaScript.