What is Destructuring?
Destructuring is a special really cool syntax feature in JavaScript, which lets us to extract values from arrays,...
For further actions, you may consider blocking this person and/or reporting abuse
I was a huge fan of destructuring as it gives me a more concise looking code. But when things get big and many objects are destructured, everything is just losing its context. Take this example
Not mentioning if you have a colliding property names then you have to make an alias for the colliding property name so you would still ended up in a messy code
Understood! I shall keep this in mind.
When using the
spread
syntax to copy objects that havenested properties
, it is crucial to understand how JavaScript handlesreferences
to thesenested properties
. Thespread operator
creates ashallow copy
of the object. This means that while the top-level properties are copied, any nested objects or arrays are stillreferenced
from the original object. Consequently, changes tonested properties
in thecopied object
will also reflect in theoriginal object
and vice versa.Let's take an example:
Here, we define an object
person
with properties likename
,age
andaddress
. Andaddress
object containscity
andzip
.Shallow Copy with Spread Operator
The spread operator
{ ...person }
creates a shallow copy ofperson
. This means that the top-level properties(name, age, and address)
are copied directly intopersonCopy
. However, sinceaddress
is anobject
(a nested property), only thereference
to this object is copied, not theactual nested object itself
.Modifying Nested Property
Since
personCopy.address
is areference
to the sameaddress
object asperson.address
, changingpersonCopy.address.city
to'Los Angeles'
affects bothperson
andpersonCopy
. Hence, bothconsole.log
statements output"Los Angeles"
.Modifying Nested Property Again
Similarly, when we change
person.address.city
back to'New York'
, it also affectspersonCopy.address.city
because theyreference
the sameaddress
object. As a result, bothconsole.log
statements output"New York"
.Massive! That's a brief write up! I learnt something new.
Thank you 😊!
Really well-explained!!
Thanks a lot for your positive feedback! I’m always looking to improve.
Thank you for your detailed feedback! Pleased to know that you found the guide comprehensive and the examples clear. Looking forward to bringing you more useful content!
And hey!
Thanks again 🙃 for your support!
Great article!!
Would love to know the performance overhead of destructuring. As js devs, we don't really care abt minor performance issue, but still it would be interesting to know.
Thank you for your kind words. You asked so I did a quick research on this topic.
I didn't actually thought about the performance consideration before. Well, it is indeed a critical point to think about.
Well as I far as I realised, Destructuring in JavaScript does introduce a slight performance overhead compared to traditional property access (dot notation) due to the extra steps involved in creating and assigning variables. In scenarios where performance is critical, this overhead can become noticeable.
For instance:
There could be more examples. But I think these are enough.
But for most practical purposes, the performance difference is negligible and should not deter you from using destructuring.
As you have been developing applications in JavaScript long before I started, you might know all of this better than I do.
This is amazing, I should also consider writing.
Thank you so much! 😊
Writing is incredibly rewarding, and I'd love to see what you come up with. Let me know if you need any suggestions on getting started, don't hesitate to reach out!
I'd love to read what you write—go for it!
awesome :)
Thank u for the appreciation :)
Well explained... It was really helpful for me
Thank you! It’s great to hear that you've found the explanation helpful!
Great! Loved it
All the lightbulbs went off above my head when you tied destructing and imports. Thank you!!!!
😊 🙌 😊 🙌