why does `var arr=[1, 1]; var x=0; arr[x]=x++` produce [0,1] and not [1,0]? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why does `var arr=[1, 1]; var x=0; arr[x]=x++` produce [0,1] and not [1,0]?

I was quite confused with this code in one of game rounds. As assignment operator is right-associative, expression x++ is evaluated first, so during arr[x] lookup x must already be 1! Why js works that way? I also checked similar code on Python and PHP, and there I got [1, 0], contradictory to js

17th Sep 2016, 5:27 PM
Andriy Maletsky
Andriy Maletsky - avatar
8 Answers
+ 3
It depends on the language, there is no standard implementation. You will only encounter those types of things in exercises and never in real life anyway.
17th Sep 2016, 6:26 PM
Zen
Zen - avatar
+ 2
Finally, according to official doc: http://www.ecma-international.org/ecma-262/5.1/#sec-11.3 1. The interpreter evaluates leftHandSideExpr. 2. after that it evaluates rightSideExpr. 3. it puts rval into lref. According that, during evaluation of lhsExpr we have x == 0. So result of lhsExpr evaluation is arr[0]. My guess was right. After all that interpreter magic goes associativity etc. That all is about how the interpreter works. So in a[i] = b = c = i++ if i == 0 you'll get a[0] == 0.
18th Sep 2016, 11:56 AM
Сергей Сарбаш
Сергей Сарбаш - avatar
+ 1
@Sergey, look, I expect something like this: 1. as '=' has the lowest predecence and is right-associative, we need to evaluate right side at first, 'x++' 2. because 'x' has value 0, expression 'x++' produces value 0. But after expression evaluation x has incremented to 1. 3. now to left part. We must find a pointer which will be bound to value from right side, 0. It is arr[x]. But x has just been set to 1, so it must be arr[1]. That is what I expect, but in fact arr[0]=0 is executed. Thanks for link, anyway.
17th Sep 2016, 9:48 PM
Andriy Maletsky
Andriy Maletsky - avatar
+ 1
it seems we do not understand each other :D 'x' var is used 2 times, on left side and on right side . I expect from postfix to increment x AFTER returning value, that's what you say. AFTER returning, but BEFORE using this x in the other side of assignment operator. PHP works with pre-, post- in the same manner. Python does not have them at all, I used function like this: `def plusplus(): global x; _x=x; x+=1; return _x`
17th Sep 2016, 10:47 PM
Andriy Maletsky
Andriy Maletsky - avatar
+ 1
Use ++x not x++
17th Sep 2016, 11:32 PM
fjlj
+ 1
Proof of concept: var arr = [ 0, 0], x = 0; arr[x++] = x; What will be the result? Let's explain. it evaluates left side to resolve lref where it'll put the result value. Here is postfix so we have arr[0] as storing place. After that the progress continues with increment evaluation. When it reaches right side to calculate expression it already has "1" in "x" variable so finally we have arr[0] == 1.
18th Sep 2016, 12:22 PM
Сергей Сарбаш
Сергей Сарбаш - avatar
0
I'm fine with that if you'll be right. :)) surely, I'm ready to take lessons and correct myself. Now, regarding our topic. I think that associativity is belongs at first to equal priority operators and an order of evaluation in that case. Here we have 2 places where "x" variable has used. As I see that before any assignment the interpreter has to calculate memory area where it will write the result. So the interpreter evaluates arr[x] at first even before calculating right side of the expression. THAT was my point behind my arguments. If that's true then the result of the expression is right and expected. The result says me that my understanding is right in this case. I just missed to sound this statement hidden in my mind. :)
18th Sep 2016, 10:59 AM
Сергей Сарбаш
Сергей Сарбаш - avatar
0
I'm still a little confused about all that!! but I m learning some things please be patient with me.!! thank you.
18th Sep 2016, 7:08 PM
Rita Adkisson
Rita Adkisson - avatar