gauyig
Last Updated: February 25, 2016
·
1.116K
· bright-day
61342aa993f6f6a3a29b0240d758a699

Javascript Quiz 1

arr = [1, 5, 3];
i = 1;
arr[i++] = arr[i++];
arr; //=>?

what is the result and why?

Say Thanks
Respond

4 Responses
Add your response

2483
Ee4b7f7f470f52385de1df68da930a2a

[1, 3, 3]

This is implementation-dependent, just so happens it gets translated to arr[1] = arr[2] by v8; I won't be surprised if e.g. Rhino or IE's JScript translates it to arr[2] = arr[1]. Like in C/C++, this sort of thing is better avoided in real code.

over 1 year ago ·
2484
736212 10200976756252420 836055407 o

mvasilkov is right. Results are browser dependent:

http://jsfiddle.net/emgiezet/rh7Wx/

over 1 year ago ·
2485
61342aa993f6f6a3a29b0240d758a699

@mvasilkov i really hadn't tested this example on other javascript interpretors but i think this shouldn't be problem.
as i know equal operator first evaluates expresion on the left: arr[1] and sets i = 2; after that evaluates right side expresion: arr[2]

over 1 year ago ·
2493
81829def637962204635704c091036ea

JavaScript is a top-to-bottom, left-to-right parsed language, so it should act like this :

arr = [1, 5, 3]
i = 1
arr[1] /* i == 2 */ = arr[2] /* i == 3 (the "++" isn't actually relevant here) */
arr
over 1 year ago ·