+ 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
4 Réponses
+ 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
+ 4
Default value only assigned, if there were no value set in the object.
+ 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.
+ 1
If 10 is the default, what part is undefined causing 42 to be in the output?