Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnthony Candaele
Full Stack JavaScript Techdegree Student 32,135 PointsI'm not sure if i fully understand this syntax: const { side } = req.query;
I believe this is called 'destructuring' but I am not sure what the part 'const { side } does, why is between brackets?
2 Answers
Steven Parker
229,732 PointsYou're exactly right, this is a destructuring assignment. The brackets indicate an object (the "structure") that "side" will be found inside of. So we can expect that the "req" object contains another object named "query" which will have a property named "side". The value associated with that property will be assigned to the new constant named "side".
Anthony Candaele
Full Stack JavaScript Techdegree Student 32,135 PointsThanks for the explanation. So
const { side } = req.query;
is the same as:
const side = req.query.side
?
Steven Parker
229,732 PointsEssentially, yes, but not as easy to read. I'd code it your way.