23

According to my tests it is always left-to-right

>> console.log( console.log(1), console.log(2) );
1
2
undefined undefined

but I can't find the relevant section confirming this in the ECMAScript standard.

3
  • Does this not follow the order of , and grouping by ( & ) ? Operator Precedence ... Commented Dec 6, 2011 at 20:03
  • console.log(1), console.log(2) are arguments and functions that are getting invoked like a self executing function
    – Joe
    Commented Dec 6, 2011 at 20:10
  • One might consider that f(g(0), g(1)) is pretty much guaranteed to do the same thing as f.apply(thisObj, [g(0), g(1)]). It's probably a useful mental device to consider the two equivalent, and perhaps it makes more sense to the OP that array elements be defined in left-to-right order. Commented Dec 6, 2011 at 20:29

3 Answers 3

21

All the operators in JavaScript evaluate their operands left-to-right, including the function call operator. First the function to call is evaluated then the actual parameters in left-to-right order.

Section 11.2.3 is the relevant spec section.

11.2.3 Function Calls

...

2 Let func be GetValue(ref).

3 Let argList be the result of evaluating Arguments, producing an internal list of argument values (see 11.2.4).

...

and you can see that the ArgumentList production is left-recursive

11.2.4 Argument lists

...

The production ArgumentList : ArgumentList , AssignmentExpression is evaluated as follows

and ArgumentList is evaluated before AssignmentExpression in the following verbiage..

Under EcmaScript 3 some of the comparison operators (<, <=, >, >=) evaluated right to left since a<=b was defined in terms of !(b<a), but that was widely recognized as a spec error, major interpreters did not implement it that way, and it was fixed in EcmaScript 5.

From the language spec:

11.8.5 The Abstract Relational Comparison Algorithm # Ⓣ

The comparison x < y, where x and y are values, produces true, false, or undefined (which indicates that at least one operand is NaN). In addition to x and y the algorithm takes a Boolean flag named LeftFirst as a parameter. The flag is used to control the order in which operations with potentially visible side-effects are performed upon x and y. It is necessary because ECMAScript specifies left to right evaluation of expressions. The default value of LeftFirst is true and indicates that the x parameter corresponds to an expression that occurs to the left of the y parameter’s corresponding expression. If LeftFirst is false, the reverse is the case and operations must be performed upon y before x. Such a comparison is performed as follows:

8
  • @ŠimeVidas, the function call operator. () Commented Dec 6, 2011 at 20:15
  • 1
    @RightSaidFred: Associativity only determines wether you disambiguate a op b op c as ((a op b) op c) or (a op (b op c). It has nothing to do with wether a or b is evaluated first in a op b
    – hugomg
    Commented Dec 6, 2011 at 20:17
  • 2
    @RightSaidFred, you are confusing associativity with order of evaluation. Associativity is taken into account at parse time, but order of evaluation is significant at runtime. Commented Dec 6, 2011 at 20:18
  • 3
    @RightSaidFred, to make this clear, try running var x = 1; (x = [])[0] = x;. x is now an array that contains itself, not an array that contains 1 because the x = [] was evaluated before the right-most x. Commented Dec 6, 2011 at 20:20
  • 3
    @RightSaidFred, meh, downvotes are a fact of life. I appreciate the question. To help you process, associativity is why x = y = z is the same as x = (y = z) but 1 - 1 - 1 is (1 - 1) - 1 instead of 1 - (1 - 1). Commented Dec 6, 2011 at 20:29
13

It's defined here:

The production ArgumentList : ArgumentList , AssignmentExpression is evaluated as follows:

  1. Let precedingArgs be the result of evaluating ArgumentList.
  2. Let ref be the result of evaluating AssignmentExpression.
  3. Let arg be GetValue(ref).
  4. Return a List whose length is one greater than the length of precedingArgs and whose items are the items of precedingArgs, in order, followed at the end by arg which is the last item of the new list.

Read here: http://es5.github.com/#x11.2.4

When a function is invoked, the passed-in arguments are evaluated from left to right.

1
  • Which, enables evaluating statements such as func(a++, ++a, a *= a, a--, --a). It is defined behavior thanks to the specification above, unlike the C language. It's not a good practice, though. Commented Feb 6, 2018 at 19:46
7

For historical interest, also see section 4.2 Evaluation Order of JavaScript 1.1 Language Specification (Brendan Eich, C. Rand Mckinney, 11/18/96).

In a function or constructor call, one or more argument expressions may appear within the parentheses, separated by commas. Each argument expression is fully evaluated before any part of any argument expression to its right is evaluated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.