Object Destructuring: Default and Parameter Values
Learn how to assign default values to object properties using object destructuring. In addition, understand the concept of extracting object properties when passed to a function as a parameter.
We'll cover the following...
Assigning default values
When extracting object properties, if we ask for a property that is not present, we can assign a default value for the missing property.
Example
Here, we’re asking for a favorite
property that is not present in the object it’s being extracted from. If the value is not present, the default value assigned to the property on the left-hand side kicks in.
Press + to interact
//START:DEFAULTconst { lat, lon, favorite = true} = {lat: 84.45, lon: -114.12};//END:DEFAULTconsole.log(`${lat} ${lon} ${favorite}`);
Explanation
-
lat
and ...