JavaScript Reference ยท Operators
The grouping operator ( )
Reference for the JavaScript grouping operator: overriding precedence, parenthesizing object literals and function expressions, IIFEs and the arrow body gotcha.
The grouping operator is a pair of parentheses around an expression. It does not change the value of the expression; it changes how the code is parsed. Whatever is inside is evaluated first and treated as one unit, which lets you override operator precedence and resolve a few spots where the parser would otherwise guess differently.
(expression)
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
( expression )
| Part | Description |
|---|---|
expression |
Any expression, including a comma expression such as (a, b). It must not be empty: () alone is not an expression. |
Return value: the value of the inner expression, unchanged. A parenthesized reference is still a reference, so (a) = 1 and (obj.x) = 1 are valid assignments.
Exceptions: none of its own.
Usage notes
- Precedence and associativity. Use parentheses whenever the default order is not obvious to a reader, even if it is correct. See the precedence table.
- Object literal results. In an arrow function,
=> ({ ... })returns an object;=> { ... }starts a block body. - Function expressions at the start of a statement. A statement beginning with the keyword
functionis parsed as a declaration, so an IIFE needs wrapping:(function () { ... })()or(() => { ... })(). Modern code uses modules and block scope instead. - Required by the language.
??cannot be mixed with||or&&without parentheses, and-x ** 2is aSyntaxError(write(-x) ** 2or-(x ** 2)). - Number literals.
5.toFixed()is a syntax error because5.is read as a decimal number;(5).toFixed()or5..toFixed()works. thisloss.(obj.method)()keepsobjasthis, because a parenthesized reference remains a reference.(0, obj.method)()does not.- Destructuring assignment. A statement starting with
{is a block, so assign to an object pattern with parentheses:({ a, b } = source);. - No runtime cost. Parentheses vanish at parse time.
Browser support
Supported everywhere, in all JavaScript environments.