+ 10
Destructuring in javascript?
This topic is playing with my mind a lot var obj = {id: 42, name: "Jack"}; Let {id = 10, age = 20} = obj; console.log(id); // 42 console.log(age); // 20 My doubt : why the id is assigned 10 .I mean why? var o = {h: 42, s: true}; var {h: foo, s: bar} = o; //console.log(h); // Error console.log(foo); // 42 mydoubt: In this code ' : ' is used in the second line of code When is ' = ' is used and when is ' : ' is used? David Carroll Luis Febro 🇧🇷 Igor Makarsky Airree
9 Answers
+ 14
':' colon is for renaming the destructure variable name, if you rename the variable h:foo you cant call previous name becuz its already rename and unavailable.
'=' equal is for assigning default value if the value of id: '' and Jack: '' is undefined the id =10 will be result and in case of age it is not in obj so its undefined age =20, default value 20 is the result
Hope this might clear.
+ 9
I suggest you to read the documentation down to the smallest detail here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
There are many examples of usage for both array and object destructuring for you to scrutinize them. This is how you will learn the topic better.
It is true what Airree related in his second paragraph. This can be annoying, pal. Send a DM for a specific person friendly and moderately and avoid tagging people here in Q&A Discussions. I know that you wanna learn a lot, but try using your energy by reading real DOCUMENTATION about JS like MDN web docs. You can be as learning hungry as you want to over there. 👍
+ 8
= means you give it an optional value. If the object property cannot be found, it will be assigned that value. : means you "rename" it. The object's "h" property will be named as foo.
Please stop tagging random people at the end, you will probably get an answer very quickly without it
+ 3
Tq Airree Luis Febro 🇧🇷 🙏
+ 2
Tq daneillonge you explained it very well😊🙏
+ 2
watch object destructure in youtube hope it will make you clear.
+ 1
Danielov is absolutely right.
0
I don't understand it
0
Cara membuat.mi?