Operator precedence defines the sequence in which operations are executed within an arithmetic expression.
Example
let x = 100 + 50 * 3; |
Does the result from the example match that of 150 * 3, or does it align with 100 + 150?
Which operation takes precedence, addition or multiplication?
Following traditional mathematical conventions, multiplication is prioritized.
Multiplication (*) and division (/) take precedence over addition (+) and subtraction (–).
Moreover, the precedence order can be altered by incorporating parentheses.
By employing parentheses, the operations enclosed within them are evaluated foremost.
Example
let x = (100 + 50) * 3; |
When multiple operations share the same precedence level (such as addition and subtraction, or multiplication and division), they are executed in a left-to-right order.
Example
let x = 100 + 50 – 3; |
let x = 100 / 50 * 3; |