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 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.

Javascript



filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


Example 2: With and without grouping operator.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // 5 * 5 + 5
    // 25+5
    // 30
    let value= 5 * 5 + 5 ; 
    console.log("Without grouping operator: " + value);
  
    // 5 * (5 + 5)
    // 5*10
    // 50
    let value1= 5 * (5 + 5);
    console.log("With grouping operator: "+ value1);
</script>                    

chevron_right



Output:

Without grouping operator: 30
With grouping operator: 50

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.