JS related question

Hi Campers,

I have a question: what is the magic hidden behind this code snippet?

let {name} = {name: "Jack", age: 22};
console.log(name); // ---> Jack

Could you please recommend me some articles to better how it works and why?

this is destructuring
let {name} will take the property name from the object and create a variable called name that holds the value from that property

this article may be what you search for:

here there is documentation on destructuring: (object destructuring is half way down the page)

1 Like

This is called Destructuring assignment . It helps us to simplifies the code by copying and assigning the value at the same time.

So here what happens is we copy the value of the name property from the object and assign to a newly created name variable.

We can also customize the name of the variable it gets stored. Check the example below

Here we are storing the value of the name in firstName variable

Check the link below to understand more about it


Hi @adi188288 and @ieahleen

Thank you guys for the explanations.

I will check the articles tomorrow and will be back if I have additional questions.