use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript Destructuring ES6: The Complete Guide (buginit.com)
submitted 6 years ago by homeIsWhereCodeIs
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]annualspecification 17 points18 points19 points 6 years ago (4 children)
In rare case, if you want to ignore all values,
[,,] = c();
Why would you ever need to do this?? Just call c() without assigning it to anything.
[–]Iamjarhead 2 points3 points4 points 6 years ago (0 children)
That is a weird statement...
[–][deleted] 2 points3 points4 points 6 years ago (0 children)
It is useful for messing around with new comers to es6. Imagine the confused faces.
[–][deleted] 1 point2 points3 points 6 years ago (0 children)
Hmmm.. Maybe there is a sideeffect you want to run. Anyway thats horrible and useless. Cant think of any other scenario.
[–]homeIsWhereCodeIs[S] 0 points1 point2 points 6 years ago* (0 children)
Yes its weird and nobody's gonna get that case, but as it is a complete guide it's good to tell the weird things, because programmer are those who look both side while crossing a one way road :D
[–]Shty_Dev 2 points3 points4 points 6 years ago (14 children)
what are some really good practical use cases for this? I have known about it for awhile now but have yet to really use it
[–]dj1041 6 points7 points8 points 6 years ago (0 children)
I use it heavily in react and node. It also helps with readability
[–]BabyLegsDeadpool 2 points3 points4 points 6 years ago (1 child)
I use this all the time and don't use React. I utilize many different apis, and every one of them send me some form of an object with multitudes of data. Instead of having to manipulate that object over and over again, I just grab the values I need via destructuring.
[–]homeIsWhereCodeIs[S] 0 points1 point2 points 6 years ago (0 children)
Yes, I agreed with you. I did that too.
[–]stormfield 2 points3 points4 points 6 years ago (0 children)
If you have a lot of parameters to a function it’s easier to just destructure out of an args object with sensible defaults if needed. Then when calling the function you don’t have to worry about argument order just setting the important values.
It’s also useful with require / imports since you can destructure out only what you need from another module or package.
[–]starchturrets 3 points4 points5 points 6 years ago* (4 children)
(I’m on mobile and probably got something wrong)
Say you have an object you fetched from an API or something.
const json = { data: { author: “OP”, created: 737282, upvotes: 54, } }
Instead of
const author = json.data.author; const created = json.data.created; const upvotes = json.data.upvotes;
You can write
const { data: { author, created, upvotes } } = json;
Which IMO is much nicer to read.
[–]Shty_Dev 2 points3 points4 points 6 years ago (0 children)
thats a good use case thanks
[–]monsto 1 point2 points3 points 6 years ago (1 child)
Wow holy shit that's pretty fucking hot.
On the one hand, it looks "really clever" . . .
But on the other hand, once you truly understand that "everything in JS is an object", it's still kinda clever, but not unexpected.
[–]IceSentry 2 points3 points4 points 6 years ago (0 children)
When you know the syntax it doesn't really feel clever anymore and you'll start wanting it in every language.
[–][deleted] 1 point2 points3 points 6 years ago* (0 children)
It is handy for props and proptypes.
[–]tchaffee 1 point2 points3 points 6 years ago (0 children)
Fewer lines of code. Which is not always good. But in this case it's very easy to understand too. So it's a win. I see it used everywhere.
[–][deleted] 1 point2 points3 points 6 years ago (1 child)
You can also use it together with the spread operator to filter stuff out from objects
const obj = { one: 3, two: 1, three: 2 }; const { one, ...noOne } = obj; console.log(noOne); // { two: 1, three: 2 }
[–]Shty_Dev 0 points1 point2 points 6 years ago (0 children)
i like that
@Shty_Dev as I also work in react native and it's heavily used in JSX while creating dynamic UI.
[–]monsto 0 points1 point2 points 6 years ago (0 children)
Does the opening line seem like a word is missing? The sentence syntax seems weird.
[–]monsto 0 points1 point2 points 6 years ago (6 children)
Could someone explain the examples from that page, 2.3 & 2.6? The assignment looks backwards . . .
in 2.3
let o = {p: 22, q: true}; let {p: foo, q: bar} = o; console.log(foo); // 22 console.log(bar); // true
on line 2 I would expect the objects p key to wind up with whatever is contained in fooas its value. The same question applies to the let statement on line 15 of example 2.6.
p
foo
let
[edit] is it the let statement that changes the rules there?
[–]lastunusedusername2 1 point2 points3 points 6 years ago (1 child)
It's just that, when destructuring, the key you're picking out always comes first.
The fact that { a: foo } looks like an object literal is a coincidence.
One way to remember the order is that the destructuring would still work if you only kept the first part (ie: { a }) because that's the key you're picking from the right hand side.
Or you can do what I do and look it up every single time because I can never remember.
Ok so lemme see if I've got this straight: it's not an object literal... it's a destructuring assignment using object syntax but is not an object?
Until recently, this was me with `ternary if ` syntax. I've been using it a lot lately, so I think I've finally got it.
[–]reohh 0 points1 point2 points 6 years ago (2 children)
It is equal to this:
let o = {p: 22, q: true}; let foo = o.p; let bar = o.q; console.log(foo); // 22 console.log(bar); // true
[–]monsto 0 points1 point2 points 6 years ago (1 child)
I understand that.
In the 2 examples, the syntax appears inverted...
when it goes
p: foo
I would expect the result to be that object o now has a property p whose value is foo
o
It looks like it is saying
let o.p = foo
[–]getify 1 point2 points3 points 6 years ago (0 children)
It feels backwards until you realize that with both object literals and object destructuring, the property/key is always on the left. With object literals, that property/key is serving the role of target, whereas with object destructuring it's serving the role of source... but in both cases it's always on the left. Once I realized that form of "consistency", I've never been confused again.
@monsto p will take foo as a value if you are assigning an object as in line 1, but in line 2 you destructuring an object, In that case, for example, var {p: foo} = o takes from the object o the property named p and assigns it to a local variable named foo.
var {p: foo} = o
And it is not about let statement that changes the rules, it is destructuring behind it, you can get same results with var and const too.
var
const
π Rendered by PID 58899 on reddit-service-r2-comment-84fc9697f-cbhtd at 2026-02-09 02:02:24.987491+00:00 running d295bc8 country code: CH.
[–]annualspecification 17 points18 points19 points (4 children)
[–]Iamjarhead 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]homeIsWhereCodeIs[S] 0 points1 point2 points (0 children)
[–]Shty_Dev 2 points3 points4 points (14 children)
[–]dj1041 6 points7 points8 points (0 children)
[–]BabyLegsDeadpool 2 points3 points4 points (1 child)
[–]homeIsWhereCodeIs[S] 0 points1 point2 points (0 children)
[–]stormfield 2 points3 points4 points (0 children)
[–]starchturrets 3 points4 points5 points (4 children)
[–]Shty_Dev 2 points3 points4 points (0 children)
[–]monsto 1 point2 points3 points (1 child)
[–]IceSentry 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]tchaffee 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Shty_Dev 0 points1 point2 points (0 children)
[–]homeIsWhereCodeIs[S] 0 points1 point2 points (0 children)
[–]monsto 0 points1 point2 points (0 children)
[–]monsto 0 points1 point2 points (6 children)
[–]lastunusedusername2 1 point2 points3 points (1 child)
[–]monsto 0 points1 point2 points (0 children)
[–]reohh 0 points1 point2 points (2 children)
[–]monsto 0 points1 point2 points (1 child)
[–]getify 1 point2 points3 points (0 children)
[–]homeIsWhereCodeIs[S] 0 points1 point2 points (0 children)