JavaScript destructuring is a powerful feature that allows developers to extract values from arrays and objects, and assign them to variables in a more concise and readable way. By using destructuring, you can easily access specific values from complex data structures, such as nested objects or arrays, without having to write lengthy code to traverse them. It’s a convenient tool that can make your code more efficient and easier to understand, once you get the hang of it.
Consider the following object:
const person = { name: "Alice", age: 30 };
To access the name property of this object, you could use either dot notation or destructuring:
// Using dot notation
console.log(person.name);
// Using destructuring
const { name } = person;
console.log(name);
In this case, both options achieve the same result. However, using destructuring is more concise and can make the code easier to read and understand. The prefer-destructuring rule would suggest using destructuring in this case.
Now, consider the following code that accesses multiple properties of an object:
const person = { name: "Alice", age: 30 };
console.log(person.name);
console.log(person.age);
This code violates the prefer-destructuring rule, as it accesses object properties using dot notation instead of destructuring. To satisfy the rule, we can use destructuring to extract the properties:
const person = { name: "Alice", age: 30 };
const { name, age } = person;
console.log(name);
console.log(age);
This code is more concise and easier to read, which makes it a better option.
Destructuring can also be used to rename properties. For example:
const person = { name: "Alice", age: 30 };
const { name: fullName } = person;
console.log(fullName);
In this code, the name property is destructured and renamed to fullName. This code satisfies the prefer-destructuring rule, as it uses restructuring.
Destructuring array elements
Consider the following array:
const numbers = [1, 2, 3, 4, 5];
To access the first element of this array, you could use either bracket notation or destructuring:
// Using bracket notation console.log(numbers[0]); // Using destructuring const [first] = numbers; console.log(first);
Again, both options achieve the same result, but using destructuring can make the code more concise and easier to read. The prefer-destructuring rule would suggest using destructuring in this case.
Now, consider the following code that accesses multiple elements of an array:
const numbers = [1, 2, 3, 4, 5]; console.log(numbers[0]); console.log(numbers[1]);
This code violates the prefer-destructuring rule, as it accesses array elements using bracket notation instead of destructuring. To satisfy the rule, we can use destructuring to extract the elements:
const numbers = [1, 2, 3, 4, 5]; const [first, second] = numbers; console.log(first); console.log(second);
This code is more concise and easier to read, which makes it a better option.
Javascript destructuring is a useful feature to improve the readability and maintainability of your code.
About the author /
Mohamed RiasI'm a programmer, photographer, and proud parent. With a passion for coding and a love of capturing life's moments through my camera lens, I'm always on the lookout for new challenges and opportunities to grow. As a dedicated parent, I understand the importance of balancing work and family, and I strive to be the best version of myself in all aspects of my life.
Popular
Editor’s Pick
-
December 6, 2012 12 Best Mid Range DSLR cameras
I am almost got fed-up to hear “What’s the best DSLR”I receive several emails from our readers asking which one they should opt. Now the true fact is, almost all DSLR cameras is good enough to take clean pictures .They all provide you with good specs, sharp images but it’s you who will decide whether…
-
January 6, 2010 45+ surreal Long Exposure Photography Inspirations
Long exposure photography is one of the coolest ways of taking pictures. It requires a longer shutter speed, anywhere from 1/2 sec up to several minutes or even a hour. The ability to take long exposures requires a user to use a tripod for optimum results (of course, some people prefer the hand shake look)….
-
September 21, 2009 150+ amazing Photo Manipulations for professional photographers
-
May 2, 2023 Implementing SOLID Principles in Your React Frontend Development
Learn how to improve the maintainability and scalability of your React frontend applications by implementing the SOLID principles. From Single Responsibility to Dependency Inversion, this article covers practical examples and best practices for writing cleaner, more modular code in your React codebase. Join us on this journey towards better frontend development with Implementing SOLID Principles in Your React Frontend Development.
-
June 10, 2012 Show Recent Posts With Thumbnail from Particular Label/Category in Blogger
Hey Blogger friends, Today i have come up with another cool blogger hack. It’s about showing recent posts with/without thumbnail from any particular label/category in blogger. As you can see in my sidebar, I have listed recent posts from each category, but i have done this in wordpress. So if you want to do same…
