ES6 (ECMAScript 2015) is one of the major updates to JavaScript. It introduced many new features that make JavaScript code cleaner, shorter, easier to read, and more maintainable.
Before ES6, developers had to write more code to accomplish certain tasks. ES6 introduced modern syntax and powerful features that simplify JavaScript development.
1. let and const
Before ES6, JavaScript developers mainly used var to declare variables.
ES6 introduced let and const.
let: Used when we want to declare a variable whose value can be changed.
let age = 25; age = 30; console.log(age); // 30 const: Used when we don't want to reassign a variable.
const name = "John"; console.log(name); // John Important differences:
- var is function-scoped.
- let and const are block-scoped.
- let allows reassignment.
- const does not allow reassignment.
- const variables must be initialized when declared.
2. Arrow Functions
Arrow functions provide a shorter way to write functions.
Before ES6
function add(a, b) { return a + b; } After ES6
const add = (a, b) => { return a + b; }; console.log(add(10, 20)); // 30 Single expression
const add = (a, b) => a + b; 3. Template Literals
Template literals allow us to insert variables and expressions directly into strings.
They use backticks instead of single or double quotes.
Before ES6
let name = "John"; let age = 25; console.log("My name is " + name + " and I am " + age); After ES6
let name = "John"; let age = 25; console.log(`My name is ${name} and I am ${age}`); NOTES:
Easier string concatenation.
Supports multiline strings.
Supports expressions inside strings.
4. Destructuring
Destructuring allows us to extract values from arrays and objects and assign them to variables.
Easy to access array elements and object properties.
Array destructuring:
const numbers = [10, 20, 30]; const [a, b, c] = numbers; console.log(a); // 10 console.log(b); // 20 console.log(c); // 30 Object destructuring:
const user = { name: "John", age: 25 }; const { name, age } = user; console.log(name); // John console.log(age); // 25 5. Default Parameters
Default parameters allow us to assign default values to function parameters.
Before ES6:
function greet(name) { name = name || "Guest"; console.log("Hello " + name); } After ES6:
function greet(name = "Guest") { console.log(`Hello ${name}`); } greet(); // Hello Guest greet("John"); // Hello John If an argument is not provided or is undefined, the default value is used.
6. Spread Operator
The spread operator (...) allows us to expand elements of an array or properties of an object.
Array example:
const arr1 = [10, 20, 30]; const arr2 = [...arr1, 40, 50]; console.log(arr2); // [10, 20, 30, 40, 50] Object example:
const user = { name: "John", age: 25 }; const updatedUser = { ...user, city: "Chennai" }; console.log(updatedUser); output:
{
name: "John",
age: 25,
city: "Chennai"
}
7. Rest Parameters
Rest parameters allow us to collect multiple arguments into a single array.
function add(...numbers) { return numbers.reduce((total, num) => total + num, 0); } console.log(add(10, 20, 30)); // 60 note : here ...numbers collects all the arguments into an array.
8. Promises
Promises were introduced in ES6 to handle asynchronous operations more effectively.
A Promise represents the eventual success or failure of an asynchronous operation.
A Promise has three states:
Pending – Operation is still in progress.
Fulfilled – Operation completed successfully.
Rejected – Operation failed.
9. Classes
ES6 introduced class syntax to make working with objects and inheritance easier.
class Person { constructor(name, age) { this.name = name; this.age = age; } greet() { console.log(`Hello ${this.name}`); } } const person1 = new Person("John", 25); person1.greet(); // Hello John notes:
class defines a class.
constructor() initializes object properties.
this refers to the current instance.
Methods define the behavior of objects.
JavaScript classes use prototype-based inheritance behind the scenes.
10. Modules (import and export)
ES6 introduced modules to split JavaScript code into separate files and reuse functionality.
Export example (math.js):
export function add(a, b) { return a + b; } Import example (app.js):
import { add } from "./math.js"; console.log(add(10, 20)); // 30 11. for...of Loop
The for...of loop allows us to iterate over iterable values such as arrays and strings.
const numbers = [10, 20, 30]; for (const num of numbers) { console.log(num); } output : Unlike a traditional index-based loop, for...of directly gives us each value.
10
20
30
12. Map
A Map is a collection of key-value pairs where keys can be of any data type.
const user = new Map(); user.set("name", "John"); user.set("age", 25); console.log(user.get("name")); // John console.log(user.get("age")); // 25 notes:
set() – Adds or updates a key-value pair.
get() – Retrieves a value.
has() – Checks whether a key exists.
delete() – Removes an entry.
clear() – Removes all entries.
13. Set
A Set is a collection of unique values. It does not allow duplicate values.
const numbers = new Set([10, 20, 20, 30, 30]); console.log(numbers); // Set(3) {10, 20, 30} Remove duplicate values of array:
const arr = [10, 20, 20, 30, 30]; const uniqueValues = [...new Set(arr)]; console.log(uniqueValues); // [10, 20, 30] 