The Wayback Machine - https://web.archive.org/web/20250115213849/https://www.geeksforgeeks.org/introduction-to-es6/
Open In App

Introduction to ES6

Last Updated : 12 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

ES6 or ECMAScript 2015 is the 6th version of the ECMAScript programming language. ECMAScript is the standardization of Javascript which was released in 2015 and subsequently renamed as ECMAScript 2015.

New Features in ES6

1. The let Keyword

The let variables are mutable i.e. their values can be changed. It works similar to the var keyword with some key differences like scoping which makes it a better option when compared to var.

JavaScript
let n = 10;
{
    let n = 2; // Block-scoped variable
    console.log(n);
}
console.log(n); 

Output
2
10

It prevents variable leakage outside of the intended scope.

2. The const Keyword

const is used to declare variables with a constant value, ensuring the value cannot be reassigned.

JavaScript
const PI = 3.14159;
PI = 3; // Error: Assignment to constant variable

Ideal for declaring configuration constants or fixed values.

3. Arrow Functions

Arrow functions provide a concise syntax for writing function expressions and automatically bind this to the surrounding context.

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

// Arrow function
const add = (a, b) => a + b;
  • Implicit return for single-expression functions.
  • Do not have their own ‘this’ context.

4. Destructuring Assignment

Destructing in JavaScript basically means the breaking down of a complex structure(Objects or arrays) into simpler parts

Object Destructuring

JavaScript
const obj = { name: "Raj", age: 25 };
const { name, age } = obj;
console.log(name, age);

Output
Raj 25

Array Destructuring

JavaScript
const a = ["red", "blue", "green"];
const [first, second] = a;
console.log(first, second);

Output
red blue

5. The Spread (…) Operator

The spread operator expands an array or object into individual elements or properties.

JavaScript
const n1 = [1, 2, 3];
const n2 = [...n1, 4, 5];
console.log(n2);

Output
[ 1, 2, 3, 4, 5 ]

6. The For/Of Loop

The for/of loop allows you to iterate over iterable objects like arrays, strings, Maps, and Sets but in a short syntax as compared to other loops.

Iterating Over an Array

JavaScript
const a = ["apple", "banana", "cherry"];
for (const fruit of a) {
    console.log(fruit);
}

Output
apple
banana
cherry

Iterating Over a String

JavaScript
const s = "hello";
for (const char of s) {
    console.log(char);
}

Output
h
e
l
l
o

7. Maps and Sets

Map: Maps store key-value pairs where keys can be any data type.

JavaScript
const map = new Map();
map.set("a", 1);
map.set("b", 2);
console.log(map.get("a"));

Output
1

Set: Sets store unique values of any type.

JavaScript
const set = new Set([1, 2, 3, 3]);
console.log(set);

Output
Set(3) { 1, 2, 3 }

8. Classes

ES6 introduced classes in JavaScript. Classes in javascript can be used to create new Objects with the help of a constructor, each class can only have one constructor inside it.

JavaScript
class Animal {
    speak() {
        console.log("The animal makes a sound");
    }
}

const dog = new Animal();
dog.speak();

Output
The animal makes a sound
  • class Animal {}: Defines a simple class named Animal.
  • speak(): A method inside the class that logs a message to the console.
  • new Animal(): Creates an object dog from the Animal class.
  • dog.speak(): Calls the speak method on the dog object.

9. Promises

Promises simplify handling asynchronous operations by providing .then and .catch methods.

JavaScript
const fetch = () => {
    return new Promise((resolve, reject) => {
        setTimeout(() => 
            resolve("Data fetched"), 2000);
    });
};

fetch().then(data => 
    console.log(data));

10. Default Parameters

Allows functions to have default values for parameters.

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

Output
Hello, Guest!
Introduction-to-ES6-copy

Introduction to ES6

Additional Enhancements

  • String Methods:
  • Array Methods:
    • find(): Locate the first element matching a condition.
    • findIndex(): Locate the index of the first element matching a condition.
    • from(): Convert an iterable or array-like object into an array.
  • Template Literals: Simplify string concatenation and allow embedded expressions.
  • Modules: Introduce import and export for better modularity.
  • Rest Parameters (…args): Allow functions to accept an indefinite number of arguments as an array.
  • Generators and Iterators: Enable custom iteration logic with function* and yield.
  • Binary and Octal Literals: Simplify working with binary (0b) and octal (0o) numbers.
  • Enhanced Object Literals: Provide shorthand for methods, properties, and dynamic keys.
  • WeakMap and WeakSet: Store weakly-referenced objects for specialized use cases.
  • Proxy and Reflect: Add interception capabilities and reflection utilities for objects.
  • Symbol: Introduces unique, immutable primitive data types useful for property keys.
  • Block-scoped Functions: Functions declared inside blocks respect block scope.

Browser Support

ES6 is supported by all modern browsers but not by Internet Explorer. Ensure compatibility by using transpilers like Babel for older environments.


Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

three90RightbarBannerImg