What Is ES6 in JavaScript?

ES6 is one of the most important updates in JavaScript. It introduced several features that make JavaScript code easier to write, read, and maintain. Features such as let, const, arrow functions, template literals, classes, and modules are widely used in modern JavaScript development.

If you are learning JavaScript for web development, understanding ES6 will help you write cleaner code and work with modern libraries and frameworks such as React.

In this tutorial, you will learn what ES6 is, why it was introduced, its important features, and how it differs from older JavaScript.

What Is ES6?

ES6 stands for ECMAScript 2015, the sixth edition of the ECMAScript language specification. It was released in 2015 and introduced many features that modern JavaScript developers use today.

ECMAScript is the standard that defines how JavaScript works. JavaScript is an implementation of that standard.

Before ES6, developers often used var to declare variables, traditional functions to define reusable code, and string concatenation to combine text with variable values. ES6 introduced alternatives that make many common programming tasks simpler.

For example, consider this JavaScript code written using older syntax:

var name = "Alex";

console.log("Hello, " + name + "!");

With ES6 template literals, you can write:

const name = "Alex";

console.log(`Hello, ${name}!`);

Output:

Hello, Alex!

The second example uses const and a template literal, both introduced in ES6. The code is easier to read, especially when combining text with multiple variables.

Why Was ES6 Introduced?

JavaScript became increasingly important as websites and web applications grew more interactive. Developers needed better ways to manage variables, organize code, work with collections, and build large applications.

ES6 introduced features to address many of these needs.

The main goals included:

  • Making JavaScript code more readable and maintainable.
  • Providing better ways to declare variables.
  • Simplifying function syntax.
  • Making string formatting easier.
  • Improving object and array handling.
  • Introducing classes for object-oriented programming.
  • Supporting modules for organizing code across files.
  • Improving asynchronous programming with promises.

These improvements helped JavaScript become more practical for developing both small websites and large applications.

Important Features of ES6

ES6 introduced many useful features. The following are some of the most important ones.

let and const

Before ES6, developers commonly used var to declare variables. ES6 introduced let and const to provide block-scoped variable declarations.

The let keyword is used when a variable’s value needs to change.

let age = 20;

age = 21;

console.log(age);

Output:

21

The const keyword is used when a variable binding should not be reassigned.

const country = "India";

console.log(country);

Output:

India

Unlike var, let and const are block-scoped. A const variable cannot be reassigned, although an object or array stored in a const variable can still have its contents modified.

Learn more in the JavaScript Variables tutorial.

Arrow Functions

Arrow functions provide a shorter syntax for writing function expressions.

Traditional function syntax:

function add(a, b) {
    return a + b;
}

console.log(add(5, 3));

ES6 arrow function syntax:

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

console.log(add(5, 3));

Output:

8

For a simple function with a single expression, you can shorten it further:

const add = (a, b) => a + b;

console.log(add(10, 20));

Output:

30

Arrow functions are widely used in callbacks, array methods, and modern JavaScript frameworks.

Learn more in the JavaScript Arrow Functions tutorial.

Template Literals

Template literals make it easier to create strings containing variables or expressions. They use backticks instead of single or double quotation marks.

const name = "Sarah";
const age = 25;

console.log(`My name is ${name} and I am ${age} years old.`);

Output:

My name is Sarah and I am 25 years old.

The ${} syntax inserts a variable or expression into the string. Template literals also support multiline strings without requiring special newline characters.

Learn more in the JavaScript Strings tutorial.

Default Parameters

ES6 allows you to assign default values to function parameters. These values are used when an argument is omitted or passed as undefined.

function greet(name = "Guest") {
    console.log(`Hello, ${name}!`);
}

greet("Alex");
greet();

Output:

Hello, Alex!
Hello, Guest!

Default parameters help reduce the need for additional checks inside functions.

Destructuring Assignment

Destructuring allows you to extract values from arrays or properties from objects and assign them to variables.

Array destructuring:

const colors = ["Red", "Green", "Blue"];

const [first, second] = colors;

console.log(first);
console.log(second);

Output:

Red
Green

Object destructuring:

const user = {
    name: "Alex",
    age: 22
};

const { name, age } = user;

console.log(name);
console.log(age);

Output:

Alex
22

Destructuring is especially useful when working with objects, arrays, function parameters, and API responses.

Spread Operator

The spread operator (...) expands the elements of an iterable, such as an array, or the enumerable properties of an object into another expression.

const firstNumbers = [1, 2, 3];
const secondNumbers = [...firstNumbers, 4, 5];

console.log(secondNumbers);

Output:

[1, 2, 3, 4, 5]

You can also use it to copy an array:

const original = [10, 20, 30];
const copy = [...original];

console.log(copy);

Output:

[10, 20, 30]

The array copy is shallow, meaning nested objects and arrays are still shared by reference.

Rest Parameters

Rest parameters allow a function to collect multiple arguments into a single array. They also use the ... syntax, but their purpose differs from the spread operator.

function addAll(...numbers) {
    return numbers.reduce((total, number) => total + number, 0);
}

console.log(addAll(10, 20, 30));

Output:

60

Here, numbers contains all the arguments passed to the function. The reduce() method calculates their total.

Classes

ES6 introduced class syntax for defining objects and their behavior. JavaScript classes use prototype-based inheritance behind the scenes.

class Person {
    constructor(name) {
        this.name = name;
    }

    greet() {
        console.log(`Hello, ${this.name}!`);
    }
}

const person = new Person("Alex");

person.greet();

Output:

Hello, Alex!

The constructor() method initializes a new object, while greet() defines a method that instances of the class can use.

Modules

ES6 introduced a standard module system using export and import. Modules allow developers to organize code into separate files and reuse functions, classes, and other values.

For example, create a file named math.js:

export function add(a, b) {
    return a + b;
}

Then import the function into another file, such as app.js:

import { add } from "./math.js";

console.log(add(5, 7));

Output:

12

These examples assume your environment supports JavaScript modules. In a browser, you can load the entry file using a script element with type="module".

Modules help make large projects easier to organize, test, and maintain.

Promises

Promises provide a standard way to represent the eventual success or failure of an asynchronous operation. They were introduced in ES6 and are commonly used when working with network requests and other asynchronous tasks.

const task = new Promise((resolve, reject) => {
    resolve("Task completed successfully!");
});

task.then((message) => {
    console.log(message);
});

Output:

Task completed successfully!

The resolve() function fulfills the promise, and .then() handles the fulfilled result. The reject() function can be used to indicate failure.

Promises are an important foundation for learning modern asynchronous JavaScript.

ES5 vs ES6

ES5 and ES6 refer to different editions of the ECMAScript standard. ES5 was released in 2009, while ES6 (ECMAScript 2015) introduced a major collection of new language features.

FeatureES5ES6
Variable declarationsCommonly uses varIntroduces let and const
Function syntaxTraditional functionsAdds arrow functions
String formattingOften uses concatenationAdds template literals
Default parametersUsually handled manuallySupports default parameter values
DestructuringNot supported nativelySupports array and object destructuring
Spread and rest syntaxNot supported nativelySupports spread and rest syntax
ClassesOften uses constructor functions and prototypes directlyAdds class syntax
ModulesNo standard import and export syntaxAdds standard modules
PromisesNo native Promise APIIntroduces native promises

ES6 did not replace JavaScript. Instead, it expanded the language with additional capabilities while retaining support for many older JavaScript patterns.

Is ES6 Still Used Today?

Yes. ES6 features remain a fundamental part of modern JavaScript development.

Developers use let, const, arrow functions, template literals, destructuring, spread syntax, classes, modules, and promises in everyday programming.

JavaScript has continued to evolve beyond ES6. Later ECMAScript editions introduced additional features, including optional chaining, nullish coalescing, and other improvements.

When learning JavaScript, it is useful to understand ES6 first and then explore newer f