ES6 (ECMAScript 2015) introduced several powerful features to JavaScript, making it more modern, readable, and maintainable. ES6 features:
🔹 1. let and const
let– block-scoped variableconst– block-scoped constant (can't be reassigned)
let count = 5;
const API_KEY = 'abc123';
🔹 2. Arrow Functions (=>)
- Shorter syntax for writing functions
- Lexically binds
this
const sum = (a, b) => a + b;
🔹 3. Template Literals
- Use backticks
`for multi-line strings and embed variables
const name = 'Teekam';
console.log(`Hello, ${name}!`);
🔹 4. Destructuring
- Extract values from arrays or objects easily
const [a, b] = [1, 2];
const {name, age} = {name: 'John', age: 25};
🔹 5. Default Parameters
- Set default values for function parameters
function greet(name = 'Guest') {
console.log(`Hello, ${name}`);
}
🔹 6. Rest and Spread Operators (...)
- Rest: Collects arguments
- Spread: Expands elements
// Rest
function sum(...args) {
return args.reduce((a, b) => a + b);
}
// Spread
const arr1 = [1, 2];
const arr2 = [...arr1, 3, 4]; // [1, 2, 3, 4]
🔹 7. Enhanced Object Literals
- Shorthand syntax in objects
const name = 'Teekam';
const person = {
name,
greet() {
console.log(`Hi, I'm ${this.name}`);
}
};
🔹 8. Classes
- Introduces class-based syntax for object-oriented programming
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, ${this.name}`);
}
}
🔹 9. Promises
- Handles async operations with
.then()and.catch()
fetch('https://api.com/data')
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err));
🔹 10. Modules (import/export)
- Reuse code across files
// math.js
export const add = (a, b) => a + b;
// main.js
import { add } from './math.js';
🔹 11. for...of Loop
- Loop over iterable objects (like arrays)
for (const item of ['a', 'b', 'c']) {
console.log(item);
}
✅ Bonus: Other Notable Features
- Block-scoped functions
Map,Set,WeakMap,WeakSetSymboltypenew.target- Tail call optimization (in strict mode)