...
/Solution Review: Destructure Undefined
Solution Review: Destructure Undefined
This lesson will explain the solution to the problem in the previous lesson.
We'll cover the following...
We'll cover the following...
The challenge in the previous lesson requires you to fix the following code using your understanding of object destructuring.
Press + to interact
Node.js
function pointValues(point){const {name:n,age:a} = pointconsole.log(n)console.log(a)}var point = {name:"jerry", age: 2}pointValues(point)point = undefinedpointValues(point)
Solution 1 #
Press + to interact
Node.js
function pointValues(point){const {name:n,age:a} = {...point}console.log(n)console.log(a)}pointValues({name:"jerry", age:2})pointValues(undefined)
Explanation #
The original code displays the name
and age
values of point
if it is defined as an object; however, it gives an error if it is undefined
. The ...