Precedence
Determines how operators are parsed concerning each other.
Operators with higher precedence become the operands of operators with lower precedence.
Example 1
Precedence of +
and -
are both 14,
Precedence of *
and /
are both 15
And =
has relatively low precedence, which is 3.
1 | console.log(3 + 4 * 5); // 3 + 20 |
Example 2
But, when Precedence of operators are equal, then follow the Associativity direction.
For example,
1 | 1+2+4 // left-to-right Associativity for '+' |
Example 3
1 | console.log(1<2<3); // true |
for <
, Associativity is left-to-right
,
Therefore, in terms of console.log(1<2<3);
1 | count 1<2, return true |
Same concept, for console.log(3>2>1);
1 | count 3>2, return true, but |