← Back to challenges

ES6: Destructuring Objects IV

JavaScriptHardlanguage_fundamentalsobjects

Instructions

const obj =  { first: "James", last: "Baker", alias: "JB"  }

var { first = "John", last = "Doe", alias } = obj

console.log(nickname) // outputs nickname is not defined

There may be times where we would like the property name to be different from the object property names we receive and also give those new property names a default value.

Question

Use ES6 object destructuring to rename the variable alias to nickname and give nickname a default value of "JD". Ignore the .toString() function (used for validation).

Notes

  • Use double quotes for "JD".
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Sum of the Items in an Array