Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript evaluation order for operators

Which of the following expressions will always precede left to right in all browsers(particularly IE6+, F3+, Opera 9+, Chrome)? For example the window should always alert first function then second function. In C they always suggest not to depend on the order of the evaluation of expressions. Is the same true for JavaScript or is Operator Precedence consistent?

function first(){
    alert('first function');
    return 0;
}
function second(){
    alert('second function');
    return 23;
}
first() + second();
first() - second();
first() * second();
first() / second();
first() < second();
first() > second();

Using mozilla it appears function evaluation should be consistent in all browsers, but obviously the standard isn't always followed.

Testing

After some test on browsershots.org it appears all browsers follow the standard.
Generally The exception is when relying on the the valueOf method in javascript. ValueOf definitely appears to be called backwards in specific cases for google chrome.

// The following alerts second then first in google chrome
first.valueOf = function(){alert('first');};
second.valueOf = function(){alert('second');};
first > second;
like image 785
Lime Avatar asked May 10 '11 02:05

Lime


People also ask

In which order the operators are evaluated?

An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first. Precedence can also be described by the word "binding." Operators with a higher precedence are said to have tighter binding.

What is the order of precedence in evaluating the operators?

Operators with the same precedence are evaluated from left to right. To override the normal order of evaluation in an expression, use parentheses. Subexpressions in parentheses are evaluated before the other parts of the expression, from left to right.

What is the order of operations in JavaScript?

Operator Precedence This stands for “Parentheses, Exponents, Multiplication, Division, Addition, Subtraction” – the order in which mathematical operations must be executed.

What is the order of execution of operators?

Operators in an expression that have higher precedence are executed before operators with lower precedence. For example, multiplication has a higher precedence than addition. In the expression 2+3*4, the multiplication is done before the addition, producing a result of 14.


2 Answers

ECMAScript 5 specifies the order of evaluation of the operands for all operators. In the case of every operator in your code snip the evaluation order is left-to-right. I'm not sure anyone could answer about the behavior of all browsers though.

Edit: See also ECMAScript 3. Evaluation order is defined the same way.

like image 52
Jake T. Avatar answered Sep 25 '22 03:09

Jake T.


Evaluating the expression into a value (e.g. involving a function call) is always done left to right.

However, once you are comparing two values, they are not converted into primitives in order to do the actual comparison in a left to right fashion. Try the following in Chrome, for example:

var l = {valueOf: function() { alert("L"); }};
var r = {valueOf: function() { alert("R"); }};

l < r; //alerts "L", then "R"
l > r; //alerts "R", then "L"
like image 44
Claudiu Avatar answered Sep 25 '22 03:09

Claudiu