Search⌘ K

Object Destructuring: Default and Parameter Values

Explore how to assign default values when destructuring objects and how to extract object properties directly as function parameters. Understand techniques to simplify code and manage missing data efficiently using object destructuring.

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.

Javascript (babel-node)
//START:DEFAULT
const { lat, lon, favorite = true} = {lat: 84.45, lon: -114.12};
//END:DEFAULT
console.log(`${lat} ${lon} ${favorite}`);

Explanation

  • lat and ...