From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Unlock this course with a free trial

Join today to access over 25,000 courses taught by industry experts.

Use object destructuring to get object properties

Use object destructuring to get object properties - JavaScript Tutorial

From the course: Learn JavaScript: Write Modern Code with JavaScript ESNext

Use object destructuring to get object properties

- The next Syntax improvement that ES6 offers over older JavaScript versions, is something called object destructuring. Despite its rather long-winded name, it has a very straightforward purpose, to make it easier to extract variables from an object's properties. To make it more clear what I mean, let's imagine that we have a person object with a lot of properties. In addition to name, age, hair color, and eye color, we might have any number of attributes such as weight, likes, dislikes, social media accounts, job title, family members, et cetera. So let's say we're writing a program that displays all these attributes of the person. This would usually mean that we have to end up doing something like we see here, where we have to manually pull out all the properties from the object. Either that, or we simply have to use the full property path like person.name, every time we want to use it, which isn't ideal either, especially if we need to do something like set a default value for some…

Contents