Operator precedence
All operators have the same precedence, which is controlled by the lack of whitespace between the operands.
1
1+2 * 3+4 # means: (1+2) * (3+4)
Copied!
In the above example, the lack of whitespace between 1, + and 2, classifies the operation as a distinct expression.
The implications are the following:
1
var n = 1 + 2 # incorrect -- it means: (var n = 1) + 2
2
var n = 1+2 # correct
3
var n = (1 + 2) # correct
Copied!
When no precedence is defined, the order of operations is from left to right:
1
1 + 2 * 3 # means: ((1 + 2) * 3)
Copied!
On the other hand, when too much precedence is defined, the order is from right to left:
1
1+2*3 # means: (1 + (2 * 3))
Copied!
The precedence can also be controlled by backslashing or preceding the operator with a dot.
1
1 + 2 \* 3 # means: (1 + (2 * 3))
2
1 + 2 .* 3 # =//=
Copied!
Last modified 1yr ago
Copy link