JavaScript operator precedence rules

Understand the basics of JavaScript operator precedence rules

Every complex statement introduces priority issues.

Take this:

const a = 1 * 2 + 5 / 2 % 2

The result is 2.5, but why? Which operations are performed first, and which ones need to be waited for?

Some operations have higher priority than others. The priority rules are listed in this table:

operator description
- + ++ -- Unary operator, increment and decrement
* / % Multiply/divide
+ - Add/subtract
= += -= *= /= %= **= operation

Operations at the same level (e.g.+with-) Execute in the order found

Following this table, we can solve this calculation problem:

const a = 1 * 2 + 5 / 2 % 2
const a = 2 + 5 / 2 % 2
const a = 2 + 2.5 % 2
const a = 2 + 0.5
const a = 2.5

Download mine for freeJavaScript beginner's manual


More js tutorials: