JavaScript Grouping Operator
The Grouping operator consists of a pair of parentheses around an expression or sub-expression to override the normal operator precedence so that expressions with lower precedence can be evaluated before an expression with higher priority. This operator can only contain expressions. The parameter list is passed to a function within this operator which will treat it as an expression.
Syntax:
( )
This ( ) operator controls the precedence of evaluation in expressions
Example 1: Below is an example of the Grouping operator.
Javascript
function gfg() {
let value1 = 3 * (2 + 3);
let value2 = (3 + 2) * 3;
console.log(value1);
console.log(value2);
}
gfg();
|
Example 2: Function as a statement and exception. In the below code, JavaScript considers a function as a statement if it is not preceded by any other statement. But applying a grouping operator that has the highest precedence over any other operator considers the function as an expression and hence it gets fully evaluated.
JavaScript
function (x) { return x };
( function (x) { return x });
|
Output:
Uncaught SyntaxError: Function statements require a function name
Example 3: With and without grouping operator.
Javascript
function gfg() {
let value = 5 * 5 + 5;
console.log( "Without grouping operator: " + value);
let value1 = 5 * (5 + 5);
console.log( "With grouping operator: " + value1);
}
gfg();
|
Output
Without grouping operator: 30
With grouping operator: 50
We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.
Supported Browser:
- Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 3 and above
- Opera 3 and above
- Safari 1 and above
Last Updated :
07 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...