+ 1

Why is id considered undefined?

The last part of the destructuring lecture talked about setting default values in case of undefined. id is set to 42, then = 10. However the default (42) is in the output and not the updated id (10). Why is 10 undefined? Here is the code for the step: var obj = {id: 42, name: "Jack"}; let {id = 10, age = 20} = obj; console.log(id); // 42 console.log(age); // 20

4th Apr 2019, 4:43 AM
Anthony Armfield
Anthony Armfield - avatar
4 Antworten
+ 5
In your code, 10 is the default value, not the one define in the object. In case object property is not defined from the object, it would set the value of the property to default value, which define in the destructuring
4th Apr 2019, 4:59 AM
Calviղ
Calviղ - avatar
+ 4
Default value only assigned, if there were no value set in the object.
4th Apr 2019, 5:43 AM
Calviղ
Calviղ - avatar
+ 2
Thanks! Now I understand I was focusing on the wrong value. age is the one that is undefined and therefore the default (20) is being used.
4th Apr 2019, 11:31 AM
Anthony Armfield
Anthony Armfield - avatar
+ 1
If 10 is the default, what part is undefined causing 42 to be in the output?
4th Apr 2019, 5:00 AM
Anthony Armfield
Anthony Armfield - avatar