offline version v3


⊗jsPmDstODV 305 of 502 menu

Default values when destructuring objects in JavaScript

You can also specify default values when destructuring objects. In this case, unlike array destructuring, any variable can be optional - not necessarily from the end of the array. Let's, for example, specify a default value for the variable year:

let obj = { month: 12, day: 31, }; let {year = 2025, month, day} = obj; console.log(year); // shows 2025 console.log(month); // shows 1 console.log(day); // shows 31

In the following code, parts of the object are written to the corresponding variables:

let options = { width: 400, height: 500, }; let color; if (options.color !== undefined) { color = options.color; } else { color = 'black'; } let width = options.width; let height = options.height;

Rework this code through destructuring according to the learned theory.

enru