Below is the example of the Grouping operator.
<script> function gfg() { // 3 * (2 + 3) let value1= 3 * (2 + 3); // (3 + 2) * 3 let value2= (3 + 2) * 3; document.write(value1 + "</br>" + value2); } gfg(); </script> |
15 15
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 function within this operator which will treat it as an expression.
Syntax:
( )
This ( ) operator controls the precedence of evaluation in expressions
Below examples illustrates the Grouping operator in JavaScript:
Example 1: 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.
<script> function(x){ return x }; // SyntaxError: Function statements // require a function name. // function as expression (function(x){ return x }); // This will run without any exception. </script> |
Example 2: With and without grouping operator.
<script> function gfg() { // 5 * 5 + 5 // 25+5 // 30 let value= 5 * 5 + 5 ; document.write("Without grouping operator: " + value); // 5 * (5 + 5) // 5*10 // 50 let value1= 5 * (5 + 5); document.write("</br>With grouping operator: "+ value1); } gfg(); </script> |
Output:
Without grouping operator: 30 With grouping operator: 50
Recommended Posts:
- Ternary operator vs Null coalescing operator in PHP
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript | ‘===’ vs ‘==’Comparison Operator
- JavaScript typeof operator
- Difference between == and === operator in JavaScript
- Arrow operator in ES6 of JavaScript
- JavaScript | Spread Operator
- What is the !! (not not) operator in JavaScript?
- JavaScript Instanceof Operator
- JavaScript Ternary Operator
- How to get negative result using modulo operator in JavaScript ?
- Difference between != and !== operator in JavaScript
- What is JavaScript >>> Operator and How to use it ?
- JavaScript Nullish Coalescing Operator
- JavaScript in operator
- JavaScript Comma operator
- JavaScript | Pipeline Operator
- Operator precedence in JavaScript
- What does +_ operator mean in JavaScript?
- JavaScript Unsigned Right Shift Assignment Operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


