Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of evaluation for function arguments in Javascript?

Tags:

javascript

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.

like image 267
hugomg Avatar asked Dec 06 '11 19:12

hugomg


People also ask

Does order of arguments matter JavaScript?

The important takeaway here is that the name of the argument doesn't have any significance. The only thing that matters is the order in which the arguments are passed.

Does order of arguments matter for a function?

Yes, it matters. The arguments must be given in the order the function expects them.

When calling a function the arguments should be in order?

3. Positional Arguments. During a function call, values passed through arguments should be in the order of parameters in the function definition. This is called positional arguments.

What are function arguments in JavaScript?

Arguments are Passed by ValueThe parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.


3 Answers

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:

like image 67
Mike Samuel Avatar answered Dec 27 '22 02:12

Mike Samuel


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.

like image 38
Šime Vidas Avatar answered Dec 27 '22 00:12

Šime Vidas


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.

like image 21
MikeM Avatar answered Dec 27 '22 01:12

MikeM