ES6 (ECMAScript 6 or ECMAScript 2015) is a major update to JavaScript that brought powerful and modern features to make coding easier, cleaner, and more efficient. It was officially released in June 2015 by ECMA International.
ES6 features:
1. let and const
let: Declares block-scoped variables.const: Declares block-scoped constants (cannot be reassigned).
let count = 10;
const pi = 3.14;
✅ Replaces var which is function-scoped and leads to issues like hoisting bugs.
2. Arrow Functions
Shorter syntax for writing functions. Also, they do not bind their own this, which helps in callbacks.
const add = (a, b) => a + b;
Equivalent to:
function add(a, b) {
return a + b;
}
3. Template Literals
String interpolation using backticks (`) instead of concatenation.
let name = "John";
console.log(`Hello, ${name}!`);
✅ Allows multiline strings and embedded expressions.
4. Default Parameters
Assign default values to function parameters.
function greet(name = "Guest") {
console.log(`Hello, ${name}`);
}
5. Destructuring Assignment
Extract values from arrays or objects into variables.
// Array destructuring
const [a, b] = [1, 2];
// Object destructuring
const {name, age} = {name: "Alice", age: 25};
6. Rest and Spread Operators
- Rest (
...): Collects remaining elements into an array. - Spread (
...): Spreads array/object elements.
function sum(...args) {
return args.reduce((a, b) => a + b, 0);
}
const arr = [1, 2, 3];
const newArr = [...arr, 4, 5];
7. Classes
Introduces class syntax for object-oriented programming.
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hi, I'm ${this.name}`);
}
}
8. Modules
Supports import and export for modular code.
// utils.js
export function add(x, y) {
return x + y;
}
// main.js
import { add } from './utils.js';
9. Promises
Used for asynchronous programming to avoid callback hell.
let fetchData = new Promise((resolve, reject) => {
setTimeout(() => resolve("Done!"), 1000);
});
fetchData.then(data => console.log(data));
10. Enhanced Object Literals
Shorthand for defining object properties and methods.
let name = "John";
let person = {
name,
greet() {
console.log("Hello");
}
};
11. For...of Loop
Iterates over iterable objects (like arrays, strings).
for (let char of "Hello") {
console.log(char);
}
Summary
ES6 modernized JavaScript by adding:
- Cleaner syntax (
let,const, arrow functions) - Easier code organization (modules, classes)
- Improved async handling (promises)
- More expressive data handling (destructuring, spread/rest)
It forms the foundation of modern JavaScript development.