4

If I run the following code on Firefox, I get an error:

new Number.toString;

But according to MDN, new Number should evaluate first. So the table is not correct, I think.

Let us take a look at MSDN. Above the table is written that operators are evaluated from left to right. But:

a = 1;
b = a = 2;

Now b has the value 2 which suggest evaluation from right to left. So this precedence table is also not correct.

What is the correct table?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marco de Wit
  • 2,686
  • 18
  • 22

3 Answers3

6

according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence$revision/510297#Table new Number should evaluate first. So the table is not correct I think.

The new Operator is complicated. Let's check the official language grammar: It does occur in two manifestations:

MemberExpression := new MemberExpression Arguments | …
NewExpression := new NewExpression | …

The latter, where is called without arguments, does indeed have a lesser precedence than the property accessors - so that your expression evaluates as new (Number.toString). However, when new is called with arguments (parenthesis), then it does have a greater precedence than a CallExpression and is equal to a property accessor, in which case they'd evaluate left-to-right. Indeed, the MDN table should make this more clear.

Let us take a look at MSDN: http://msdn.microsoft.com/en-us/library/z3ks45k7(v=vs.94).aspx . Above the table is written that operators are evaluated from left to right.

This is definitely wrong. Operator associativity is not always left-to-right, most obvious at the assignment operators as in your example. The MDN table states this correct. Also, MSDN seems to oversimplify the precedence of postfix operators.

Can anyone give me a correct table?

Try my new revision of MDN's table.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    The MDN table had been correct in [this revision](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence$revision/418427), but then became incorrect. I have fixed it now, please check if you can understand the new table (and whether I still have overlooked something) – Bergi Jan 16 '14 at 14:13
  • What about `new new Function` ? Associativity is needed now I think. – Marco de Wit Jan 16 '14 at 17:19
  • Yes, it is right-to-left associative: `new (new (Function)))`. Did I forgot that? – Bergi Jan 16 '14 at 17:25
  • I meant `new new Function()()` . I use it on a daily basis. ;) – Marco de Wit Jan 16 '14 at 17:26
  • That's `new (new (Function)())()`. `new` with arguments rules them all - I'd say by precedence, I don't know how to apply the concept of "associativity" to such an expression. It's a [context-free grammar](https://en.wikipedia.org/wiki/Context-free_grammar) production after all. – Bergi Jan 16 '14 at 18:13
  • Isn't that right to left then? Because the right `new` is done first? Maybe it is better to add a footnote to `new` in the table which warns about the trickyness. – Marco de Wit Jan 17 '14 at 08:54
  • It does apply the precedence rules to form an abstract syntax tree, and then evaluates that from outside in. All binary operators that always evaluate both their arguments will do so left-to-right. Conditional operators still go outside in and start on the left. – Bergi Dec 03 '17 at 10:05
  • The first and the last links are broken: *"Page not found"* – Peter Mortensen Sep 14 '22 at 13:08
  • @PeterMortensen It seems those became lost by MDN switching from their wiki to some github repo, iirc without keeping the history – Bergi Sep 15 '22 at 18:32
0

Here is MDN's full operator precedence table, available at your link:

enter image description here

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Javascript precedence are more impotant, some learning website does not give proper list, we should careful about this.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 25 '22 at 23:07
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33006917) – M.Nar Oct 27 '22 at 19:48