Math operations precedence in JavaScript

JavaScript math operations have the same precedence as normal math operations. That is, multiplication and division are done first, addition and subtraction then.

In the following example, 2 is first multiplied by 2, and then 3 is added to the result:

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

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

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

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

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

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

let a = 8 / 2 + 2; alert(a);

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

let a = 8 + 2 / 2; alert(a);

Equal precedence

Multiplication and division have the same precedence and are computed in turn from left to right. Let's look at an example to understand what is meant. The following code will first perform division and then multiplication:

let a = 8 / 2 * 4; alert(a); // shows 16 (the result of 4 * 4)

If you rearrange operations, then multiplication will be performed first, and then division:

let a = 8 * 2 / 4; alert(a); // shows 4 (the result of 16 / 4)

In the below example, each new division operation will be applied to the previous one:

let a = 16 / 2 / 2 / 2; alert(a); // shows 2

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

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

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

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

Grouping parentheses

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