I'm having a hard time understanding what is meant by the combination of Exponentiation and everything else (Multiplication, Division, etc) in group 14 of the Javascript precedence.
Source - MDN
Three questions:
The precedence of operators in JavaScript helps to determine which operation is being evaluated first than others during the parsing and execution of complex expressions. Consider the following expression in which there are two operators + and *. let result = 10 + 4 * 5 // result is 30.
The operators with the highest level of precedence in this example are multiplication and division. However, since division and multiplication have the same level of precedence, the associativity column says to read the expression from left to right first.
As one goes down the table, the precedence of these operators decreases over each other, that is, the priority of an operator is lower than the operators above it and higher than the ones below it. The operators in the same row have the same priority. In this table, 1 is the highest precedence and 19 is the lowest precedence.
Precedence is used to tell the compiler what operations should be performed first. For example, consider three numbers 2, 3, and 4. Now consider two operations: The first operation is associativity where the order does not matter.
1) 2 ** 3 ** 4
, being right-to-left associative, is 2 ** (3 ** 4)
. 2 / 3 / 4
, being left-to-right associative, is (2 / 3) / 4
.
2/3) I believe 2 ** 3 * 4
is (2 ** 3) * 4
. 2 * 3 ** 4
is 2 * (3 ** 4)
(as evaluated by es6fiddle).
This does not follow from the table; but exponentiation should have precedence over multiplication. Mixing left-to-right and right-to-left in one precedence rank is strange. In fact, as far as I could see in ES7 drafts, it is not at all treated grammatically the same way as *
, /
and %
, but as a unary operation (!).
Also note that no engines other than Babel and Traceur have support for **
at the current time, so it is mostly academic at this point. MDN is a wiki, and exponentiation operator was added by a Mozillian; but AFAIK since Mozilla doesn't currently support **
, it does not actually document the way Mozilla interprets the language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With