const myObj = {
name: "Rahul",
age: 34,
Roll: 23
}
// console.log(myObj)
const {x, y, z} = myObj;
console.log(x)
I tried to store values of myObj in these three variables using the destructuring assignment but its giving undefined.
const myObj = {
name: "Rahul",
age: 34,
Roll: 23
}
// console.log(myObj)
const {x, y, z} = myObj;
console.log(x)
I tried to store values of myObj in these three variables using the destructuring assignment but its giving undefined.
You use destructuring assignment this way:
const myObj = { name: "Rahul", age: 34, Roll: 23 }
const {name, age, Roll} = myObj;
but if you want to rename the variables with x
, y
and z
you can do this:
const myObj = { name: "Rahul", age: 34, Roll: 23 }
const {name: x, age: y, Roll: y} = myObj;
you can destructure something like this
const {name:x, age:y, Roll:x } = myObj;
You are trying to access x,y and z which are not the properties of your object