What is the order in which js assignment statements are executed when there are functions?

come across this article Operand evaluation order , which gives three examples to illustrate that" when a function has side effects, different evaluation order of operands will lead to different results "

but you can see in the operator priority table :
function calls (19, left to right) > addition (13, left to right) > assignment (3, right to left)

Why is there an order problem? Shouldn"t you perform a function call before adding?

The examples in the

article are as follows:

Code 1

var a = 5;
function fun(){
    a = 10;
    return 20;
}
var b = a + fun();
console.log("a:", a);
console.log("b:", b);
// a: 10
// b: 25

Code 2

//  a  fun 
var a = 5;
function fun(){
    a = 10;
    return 20;
}
var b = fun() + a;
console.log("a:", a);
console.log("b:", b);
// a: 10
// b: 30
< hr >

Code 3

//  fun 
var a = 5;
function fun(a){
    a = 10;
    return 20;
}
var b = fun(a) + a;
console.log("a:", a);
console.log("b:", b);
// a: 5
// b: 25
// var b = a + fun(a); 

< hr >

supplement
in terms of the tuberculous nature of the addition operator, the left value is determined first and then the right value.
but the explanation for tuberculosis goes like this: associativity determines the order in which operators with the same precedence are executed.
so priority should be considered first, and then tuberculosis should be considered, right? Or am I wrong?

Sorry, there"s too much nonsense. Repeat my question:
Why is there a problem with the order of "Code 1" and "Code 2"? Shouldn"t you perform a function call before adding?

Mar.22,2021

refer to ECMAScript 5.1Specification:
English version: http://www.ecma-international.

there is a description in Appendix D:

11.8.2, 11.8.3, and 11.8.5, the paraphrase EcmaScript is generally interpreted and executed from left to right, but the description language of the > and < = operators in the third edition of the specification results in a local right-to-left order. This specification has corrected these operators and is now interpreted in full order from left to right. However, this change to the order may be observed if there are side effects during the interpretation of the execution process.

all code in ECMAScript is executed from left to right (Evaluation order evaluation order), if you have side effects in your function, such as code in a problem, you can clearly observe "from left to right".

questions about operator precedence and evaluation order are well explained on, stack overflow:
https://stackoverflow.com/que.

The question mentioned in

Mark Lutton's answer:
operator priority and evaluation order are two different concepts. The multiplication in sqrt (9) + sqrt (16) * sqrt (25) is wrong when it runs first, and the expression always runs from left to right, but when it encounters the * operator, it will be combined first.

so in the above problems, either a + fun () or fun () + a only represents the combination of fun and () , rather than the first operation.

in the addition operation, the left value is determined first, and then the right value is determined.

so, in code 1, when determining the left value, var 5, the statement can be understood as fun () operation after var b = 5 + fun ();, a becomes 10, return 20, so var b = 5 + 20; but a becomes 10 in fun () operation, b is 25.

In

code 2, fun () return 20 is determined when the left value is determined, a becomes 10, and then the right value is determined. An is already 10, so it is equivalent to 20 + 10 = 30

. The parameter an of, fun (a) in

code 3 is not the same as the external variable a, and parameter an is a new variable. (the right value of js call by value), fun (a) + an is not affected by fun (a) on the left, so it is: 20 + 5 / 5 + 20

. The order of

JS syntax is different from the others, not from top to bottom or left to right, but from the simplest statement

.
Menu