Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parentheses in an object literal

Are parentheses in an object literal simply the grouping operator?

node-stringify will convert:

 [ { a: 1 } ]

to the string:

 [({'a':1}),({'a':2})]

Can I take it that the parentheses here have no impact to the data, ie it is totally the same even if the parentheses are absent?

like image 372
Old Geezer Avatar asked Dec 04 '25 15:12

Old Geezer


1 Answers

Yes, (...) in this case is simply being used for grouping an expression. Omitting the parentheses would have no impact on your current data structure.

Parentheses can become more useful in situations where object literals might be interpreted as a block statement instead, e.g. when evaluating an expression in the developer console or when used inside ES6 Arrow functions:

const first = () => {a: 1}
const second = () => ({a: 1})

console.log(first()) //=> undefined
console.log(second()) //=> {a: 1}

I suspect this is why node-stringify has nominated to include them in its output ― to avoid ambiguity wherever possible.

like image 107
gyre Avatar answered Dec 07 '25 05:12

gyre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!