JavaScript Es6+ Features Detailed Guide
JavaScript Es6+ Features Detailed Guide
ES6+ Features
This document provides detailed research notes on key features introduced in ECMAScript
2015 (ES6) and subsequent versions (ES7, ES8, ES9, etc.), often collectively referred to as
ES6+. These features have significantly enhanced JavaScript's capabilities, readability, and
developer experience.
4. Destructuring Assignment
Destructuring assignment allows you to unpack values from arrays or properties from objects
into distinct variables.
Array Destructuring
const colors = ['red', 'green', 'blue'];
// Destructuring array elements
const [firstColor, secondColor, thirdColor] = colors;
console.log(firstColor); // Output: red
console.log(secondColor); // Output: green
// Skipping elements
const [,, lastColor] = colors;
console.log(lastColor); // Output: blue
// Swapping variables easily
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b); // Output: 2 1
Object Destructuring
const person = {
firstName: 'John',
lastName: 'Doe',
age: 40
};
// Destructuring object properties
const { firstName, age } = person;
console.log(firstName); // Output: John
console.log(age); // Output: 40
// Renaming properties during destructuring
const { firstName: fName, lastName: lName } = person;
console.log(fName); // Output: John
console.log(lName); // Output: Doe
// Destructuring with default values
const { city = 'New York', age: personAge } = person;
console.log(city); // Output: New York (since 'city' not in
person)
console.log(personAge); // Output: 40
6. Classes
ES6 introduced class syntax, which is syntactic sugar over JavaScript's existing
prototype-based inheritance. It provides a cleaner and more familiar way to create objects and
handle inheritance.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // Call the parent constructor
this.breed = breed;
}
speak() {
console.log(`${this.name} barks!`);
}
fetch() {
console.log(`${this.name} fetches the ball.`);
}
}
const myAnimal = new Animal('Generic Animal');
myAnimal.speak(); // Output: Generic Animal makes a sound.
const myDog = new Dog('Buddy', 'Golden Retriever');
myDog.speak(); // Output: Buddy barks! (overridden method)
myDog.fetch(); // Output: Buddy fetches the ball.
7. Modules (import / export)
ES6 introduced a standardized module system, allowing developers to break down code into
reusable units and manage dependencies.
Note: Module import/export requires a build step (like Webpack or Parcel) or a browser that
supports ES Modules (using <script type="module">).
8. Promises
Promises provide a cleaner way to handle asynchronous operations, avoiding "callback hell." A
Promise represents the eventual completion (or failure) of an asynchronous operation and its
resulting value.
function fetchData() {
return new Promise((resolve, reject) => {
// Simulate an asynchronous operation (e.g., fetching data
from an API)
setTimeout(() => {
const success = true; // Change to false to simulate an
error
if (success) {
resolve('Data successfully fetched!');
} else {
reject('Error fetching data.');
}
}, 2000); // 2-second delay
});
}
fetchData()
.then((data) => {
console.log('Success:', data);
return 'Processed: ' + data; // Chain another .then
})
.then((processedData) => {
console.log('Second then:', processedData);
})
.catch((error) => {
console.error('Error:', error);
})
.finally(() => {
console.log('Operation finished (resolved or rejected).');
});
console.log('Fetching data...'); // This will log immediately
● Set: A collection of unique values. Any duplicate values added to a Set are ignored.
Maintains insertion order.
const mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add('text');
mySet.add(1); // Duplicate, ignored
console.log(mySet.size); // Output: 3
console.log(mySet.has(5)); // Output: true
console.log(mySet.has(10)); // Output: false
const numbersWithDuplicates = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbersWithDuplicates)];
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5]