

What is Destructuring in ES6 ?
source link: https://www.geeksforgeeks.org/what-is-destructuring-in-es6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is Destructuring in ES6 ?
Destructuring in ES6 is a convenient feature that allows you to extract values from arrays or objects and assign them to variables in a more concise and readable way. It simplifies the process of extracting specific data from complex structures.
Destructuring Arrays:
Basic Array Destructuring:
const numbers = [1, 2, 3];
// Destructuring assignment
const [a, b, c] = numbers;
console.log(a); // Output: 1
console.log(b); // Output: 2
console.log(c); // Output: 3
Destructuring array with the help of spread operator:
const numbers = [1, 2, 3, 4, 5];
// Skipping the second element
const [first, , third, ...rest] = numbers;
console.log(first); // Output: 1
console.log(third); // Output: 3
console.log(rest); // Output: [4, 5]
Destructuring Objects:
Basic Object Destructuring:
const person = { name: 'John', age: 30 };
// Destructuring assignment
const { name, age } = person;
console.log(name); // Output: John
console.log(age); // Output: 30
Alias (Renaming Variables):
const person = { name: 'John', age: 30 };
// Destructuring assignment with alias
const { name: personName, age: personAge } = person;
console.log(personName); // Output: John
console.log(personAge); // Output: 30
Default Values:
const person = { name: 'John' };
// Destructuring assignment with default value
const { name, age = 25 } = person;
console.log(name); // Output: John
console.log(age); // Output: 25 (default value)
Destructuring Function Parameters:
// Destructuring function parameters
function printPersonDetails({ name, age }) {
console.log(`Name: ${name}, Age: ${age}`);
}
const person = { name: 'Alice', age: 28 };
printPersonDetails(person);
// Output: Name: Alice, Age: 28
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Recommend
-
95
I recently had the unfortunate task of parsing large amounts of text from a large set of badly formatted .csv files. Java’s built-in support for regular expressions (found in thejava.util.regex…
-
73
Use advanced array destructuring techniques for more expressive handling of arrays in PHP. New array functionalities since version 7.1
-
84
I’ve replaced the ES5 callback function with an ES6 fat arrow. Because there’s only one argument, I can omit the braces. Because there’s only one line, I can use the implied return statement. The…
-
44
Learning a new language seems to be an experience that you undertake to: Change jobs Because your boss made you do it Because you are a "nerd" The thing I forget each...
-
57
Destructuring is a powerful way to create or assign variables from values inside arrays and objects. It will make your code simpler! Use it to setup options in a function in an easy way.
-
26
Destructuring declarations, or destructuring for short, is a technique for unpacking a class instance into separate variables. This means that you can take an object, and create standalone variables from its...
-
29
Destructuring declarations, or destructuring for short, is a technique in Kotlin for unpacking a class instance into separate variables.
-
35
What is Destructuring? Destructuringis a convenient way of accessing multiple properties stored in objects and arrays. It...
-
43
ES6 added many great features to JavaScript. Among these features are template literals, destructuring and default parameters and values. These are also the features you will learn about in this part. Improve your knowled...
-
28
Destructuring Assignmentis an amazing feature introduced with EcmaScript 2016, which is now available in both browsers and Node.js. If you’re writing CommonJs or ES6 modules, you’re probably already using it! Le...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK