Sar Kumar
Sar Kumar

Reputation: 31

Javascript: Usability of curly brackets

I am having trouble understanding the use of outermost brackets () . I tried removing them but it didn't work. What are the they and how do I know more about them?

({ a, b } = { a: 10, b: 20 });

console.log(a); // 10
console.log(b); // 20

Anybody willing to shed some light is very much appreciated. Thanks in advance!

Upvotes: 2

Views: 40

Answers (2)

subtleseeker
subtleseeker

Reputation: 5253

From MDN,

Parenthesize the body of function to return an object literal expression:

params => ({foo: bar})

Also have a look at this example,

Destructuring within the parameter list is also supported

var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
f(); // 6

Hopefully, this helps to clear your doubt.

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

If you use var or let to declare the variables of destructuring assignment then it will work without (). You might be missing the declaration so it was not working for you.

var { a, b } = { a: 10, b: 20 };

console.log(a); // 10
console.log(b); // 20

Upvotes: 1

Related Questions