| Template Literals in JavaScript | Async/await deep dive | |
Destructuring in JavaScript |
Destructuring is a powerful JavaScript feature introduced in ES6 (ECMAScript 2015) that allows you to unpack values from arrays or properties from objects into distinct, readable variables. This process simplifies the extraction of data from complex data structures, making code cleaner and more concise. It does not modify the original array or object, which remains intact.
Destructuring is a feature introduced in ES6 that allows you to unpack values from arrays or properties from objects into distinct variables. It simplifies code and improves readability.
Array destructuring assigns array elements to variables based on their position.
//javascript const colors = ["red", "green", "blue"]; const [firstColor, secondColor, thirdColor] = colors; console.log(firstColor); // Output: "red" console.log(secondColor); // Output: "green" console.log(thirdColor); // Output: "blue"
// Basic example const numbers = [10, 20, 30]; const [a, b, c] = numbers; console.log(a); // 10 console.log(b); // 20 console.log(c); // 30 // Skipping elements const [x, , z] = numbers; console.log(z); // 30 // Using rest operator const [first, ...rest] = numbers; console.log(rest); // [20, 30]
You can skip unwanted elements by leaving the corresponding position empty.
//javascript const fruits = ["Bananas", "Oranges", "Apples", "Mangos"]; const [fruit1, , fruit3] = fruits; console.log(fruit1); // Output: "Bananas" console.log(fruit3); // Output: "Apples"
Destructuring provides a clean way to swap the values of two variables without a temporary variable.
//javascript let a = 1; let b = 3; [a, b] = [b, a]; console.log(a); // Output: 3 console.log(b); // Output: 1
The rest syntax (...) can be used to gather the remaining elements into a new array.
//javascript const numbers = [10, 20, 30, 40, 50, 60, 70]; const [first, second, ...rest] = numbers; console.log(first); // Output: 10 console.log(second); // Output: 20 console.log(rest); // Output: [30, 40, 50, 60, 70]
Object destructuring assigns properties of an object to variables with the same names. The order of the properties does not matter.
//javascript
const person = {
firstName: "John",
lastName: "Doe",
age: 50
};
const { firstName, age } = person;
console.log(firstName); // Output: "John"
console.log(age); // Output: 50
// Basic example
const person = { name: "Alice", age: 25 };
const { name, age } = person;
console.log(name); // Alice
console.log(age); // 25
// Renaming variables
const { name: fullName } = person;
console.log(fullName); // Alice
// Default values
const { city = "Unknown" } = person;
console.log(city); // Unknown
You can assign an object property to a variable with a different name using a colon (:).
//javascript
const { firstName: name, age: yearsOld } = person;
console.log(name); // Output: "John"
console.log(yearsOld); // Output: 50
Default values can be set for properties that may not exist in the object.
//javascript
const { firstName, country = "USA" } = person;
console.log(firstName); // Output: "John"
console.log(country); // Output: "USA" (default value used)
Destructuring can be used to extract properties from nested objects.
//javascript
const user = {
id: 1,
profile: {
username: "johnDoe",
email: "john@example.com"
}
};
const { profile: { username } } = user;
console.log(username); // Output: "johnDoe"
map() or filter() for elegant data transformations | Template Literals in JavaScript | Async/await deep dive | |