Grouping parentheses in JavaScript

Optionally, you can specify the precedence of operations using parentheses. Let's, for example, rewrite our code so that addition is performed first, and only then multiplication:

let a = 2 * (2 + 3); alert(a); // shows 10 (the result of 2 * 5)

Note: there can be any number of brackets, including nested ones:

let a = 2 * (2 + 4 * (3 + 1)); alert(a);

You can enclose in parentheses operations that have higher precedence - it will not be an error. For example, we enclose the product of numbers in the parentheses:

let a = (2 * 2) + 3; alert(a); // shows 7 (the result of 4 + 3)

In this case, the parentheses are excessive (after all, multiplication has higher precedence anyway), but the code is valid.

Sometimes such a grouping is used when the priority of operations is not obvious. For example, consider the following code:

let a = 8 / 2 * 4; alert(a);

As you already know, division will be performed in first here, and then multiplication. But at first glance it may not be too obvious.

You can use grouping parentheses to explicitly show precedence:

let a = (8 / 2) * 4; alert(a);

Determine what will be displayed on the screen without running the code:

let a = (2 + 3) * (2 + 3); alert(a);

Determine what will be displayed on the screen without running the code:

let a = (2 + 3) * 2 + 3; alert(a);

Determine what will be displayed on the screen without running the code:

let a = 2 * (2 + 4 * (3 + 1)); alert(a);

Determine what will be displayed on the screen without running the code:

let a = 2 * 8 / 4; alert(a);

Determine what will be displayed on the screen without running the code:

let a = (2 * 8) / 4; alert(a);

Determine what will be displayed on the screen without running the code:

let a = 2 * (8 / 4); alert(a);
enru