Grouping operator ( )
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thegrouping( )
operator controls the precedence of evaluation in expressions. It also acts as a container for arbitrary expressions in certain syntactic constructs, where ambiguity or syntax errors would otherwise occur.
Try it
console.log(1 + 2 * 3); // 1 + 6// Expected output: 7console.log(1 + (2 * 3)); // 1 + 6// Expected output: 7console.log((1 + 2) * 3); // 3 * 3// Expected output: 9console.log(1 * 3 + 2 * 3); // 3 + 6// Expected output: 9
Syntax
(expression)
Parameters
expression
Anyexpression to be evaluated, includingcomma-joined expressions.
Description
The grouping operator consists of a pair of parentheses around an expression that groups the contents. The operator overrides the normaloperator precedence, so that operators with lower precedence (as low as thecomma operator) can be evaluated before an operator with higher precedence.
Examples
Using the grouping operator
Evaluating addition and subtraction before multiplication and division.
const a = 1;const b = 2;const c = 3;// default precedencea + b * c; // 7// evaluated by default like thisa + (b * c); // 7// now overriding precedence// addition before multiplication(a + b) * c; // 9// which is equivalent toa * c + b * c; // 9
Notice in these examples that the order in which theoperators evaluate has changed, but the order in which theoperands evaluate has not. For example, in this code, the function invocationsa()
,b()
, andc()
are evaluated left-to-right (the normal order of evaluation) before the operator order is considered.
a() * (b() + c());
The functiona
will be called before the functionb
, which will be called before the functionc
. For more on operator precedence, see itsreference page.
Using the grouping operator to eliminate parsing ambiguity
Anexpression statement cannot start with the keywordfunction
, because the parser would see it as the start of afunction declaration. This means the followingIIFE syntax is invalid:
function () { // code}();
The grouping operator can be used to eliminate this ambiguity, since when the parser sees the left parenthesis, it knows that what follows must be an expression instead of a declaration.
(function () { // code})();
You may also use thevoid
operator to eliminate ambiguity.
In anarrow function expression body (one that directly returns an expression without the keywordreturn
), the grouping operator can be used to return an object literal expression, because otherwise the left curly brace would be interpreted as the start of the function body.
const f = () => ({ a: 1 });
If a property is accessed on a number literal, theproperty accessor dot.
may be ambiguous with a decimal point, unless the number already has a decimal point. You can wrap integer literals in parentheses to eliminate this ambiguity.
(1).toString(); // "1"
Grouping operator and automatic semicolon insertion
The grouping operator can mitigateautomatic semicolon insertion (ASI) pitfalls. For example, thereturn
keyword and the returned expression cannot have a line break in between:
function sum(a, b) { return a + b;}
This code will returnundefined
, because a semicolon is inserted directly after thereturn
keyword, which causes the function to return immediately without evaluatinga + b
. In case the returned expression is long and you want to keep it well-formatted, you may use the grouping operator to signify that thereturn
keyword is followed by an expression and prevent semicolon insertion:
function sum(a, b) { return ( a + b );}
However, grouping may alsointroduce ASI hazards. When a line starts with a left parenthesis and the previous line ends with an expression, the parser will not insert a semicolon before the line break, because it could be the middle of a function call. For example:
const a = 1(1).toString()
This code would be parsed as:
const a = 1(1).toString();
Which throws "TypeError: 1 is not a function". If your coding style does not use semicolons, remember that when a line starts with a left parenthesis,prefix it with a semicolon. This practice is recommended by several formatters and/or style guides, includingPrettier andstandard.
const a = 1;(1).toString()
For more advice on working with ASI, see itsreference section.
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-grouping-operator |