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 - 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
-
-
-
-
-
-
-
-
(Locked)
Learning objectives1m 45s
-
(Locked)
Write functions using arrow syntax4m 22s
-
Set default values for function arguments3m 32s
-
(Locked)
Learn about the spread operator4m 11s
-
(Locked)
Use object destructuring to get object properties3m 53s
-
(Locked)
Use interpolation to insert data into strings1m 44s
-
(Locked)
Learn the import and export code in JavaScript6m 19s
-
(Locked)
-
-
-
-
-
-