[JavaScript] Object & Array Destructuring
![[JavaScript] Object & Array Destructuring](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1685178771666%2F662fdc18-29cb-421e-87ae-eb6812ee3d1f.png&w=3840&q=75)
Computer Science Enthusiast with a Drive for Excellence | Data Science | Web Development | Passionate About Tech & Innovation
Introduction
In this article, we will be learning about object and array destructuring, which makes your code more readable and organized. Object & Array destructuring help you to avoid writing a long code to do a simple job.
For example:
// Printing first two elements in an array
let arr = [10, 20, 30, 40, 50];
for (let i = 0; i < 2; i++) {
console.log(arr[i]);
}
// =======================================
// Array Destructuring
let a, b;
[a, b] = arr;
console.log(a); // 10
console.log(b); // 20
It's called destructuring because you are distributing the data in an array/object and assign the elements to other variables.
With an array destructuring, you can declare a variable and store the data. This is also a benefit of destructuring because it allows you to store a specific value in a variable and you can use it later. If you are working with a team, more readable and organized code would be great.
Array Destructuring
let arr = [10, 20, 30, 40, 50];
let a, b, rest;
[a, b, ...rest] = arr
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]
Variable a and b stores the first two elements of the array, and the rest is assigned to the rest variable.
You might get a syntax error if there is a comma at the end:
let arr = [10, 20, 30, 40, 50];
let a, b, rest;
[a, b, ...rest,] = arr // SyntaxError: rest element may not have a trailing comma
Object Destructuring
Object destructuring is the same as an array. Let's just have a look at the example.
Object destructuring:
let person = {
name: "John Doe",
email: "john@gmail.com",
gender: "male"
};
let {name, email, gender} = person;
console.log(name); // John Doe
console.log(email); // john@gmail.com
console.log(gender); // male
Reference
Reference: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
![[CS fundamentals] Linked List Creation & Insertion explained in C.](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6a74e5c0-44ca-4110-9ee7-e8e67767a9dc.png&w=3840&q=75)
![[CS Fundamentals] Pointers in C](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6f81f5a6-d87c-4382-883f-fa4d0a8b5968.png&w=3840&q=75)
![[SQL] Writing Efficient SQL Queries](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1751292603168%2Fb0b43c7d-8e08-47b2-a64c-b600e3511831.jpeg&w=3840&q=75)
![[CS Fundamentals] Binary Search](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1747041878054%2Fc8d8adb9-097d-484b-9562-f43e4bce9a46.png&w=3840&q=75)
![[CS Fundamentals] Sorting Algorithms](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1746973590876%2Ffab37371-1496-42ef-950b-432e7637c89e.png&w=3840&q=75)