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?
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?
Share Improve this question edited Sep 14, 2022 at 13:03 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jan 16, 2014 at 10:17 Marco de WitMarco de Wit 2,8042 gold badges19 silver badges23 bronze badges 3- Associativity, it matters. – raina77ow Commented Jan 16, 2014 at 10:20
-
3
Regarding why
new Number.toString()
is incorrect (not just Gecko) stackoverflow./questions/21100001/… – marekful Commented Jan 16, 2014 at 10:21 - 2 I am looking for a precedence table which explains that. – Marco de Wit Commented Jan 16, 2014 at 10:31
3 Answers
Reset to default 6according to https://developer.mozilla/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 plicated. 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./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.
Here is MDN's full operator precedence table, available at your link:
Javascript precedence are more impotant, some learning website does not give proper list, we should careful about this.