I am currently learning JS here and have the following code, but I do not understand why it is returning the name property.
let {name} = {name: "Faraji", age: 23};
console.log(name);
// → Faraji
And when I remove the curly braces from the variable name, I get [object Object]. Why?
let name = {name: "Faraji", age: 23};
console.log(name);
// → [object Object]
And when I access the age property with the braces I get undefined. Why?
let {name} = {name: "Faraji", age: 23};
console.log(name.age);
// → undefined
And accessing age without the braces returns undefined, too. But why?
let name = {name: "Faraji", age: 23};
console.log(name.age);
// → undefined
Whereas changing name
to name1
works as expected. Thats nice.
let name1 = {name: "Faraji", age: 23};
console.log(name1.age);
// → 23