Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript operator precedence technicality

Tags:

javascript

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:

  1. What is the meaning of combining the right-to-left and left-to-right associativity in one group?
  2. How can the expression 2 ** 3 * 4 be rephrased according to these rules, and still get the same answer? 4 * 2 ** 3 works...is that what's meant?
  3. When / how is this not equivalent to the seemingly simpler expedient of giving exponentiation higher precedence?
like image 538
NessBird Avatar asked Oct 07 '15 04:10

NessBird


People also ask

What is the precedence of operators in JavaScript?

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.

Which operator has the highest level of precedence in the table?

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.

How do you find the precedence of an operator?

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.

What is precedence in C++?

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 Answers

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.

like image 111
Amadan Avatar answered Oct 29 '22 21:10

Amadan