Javascript Quiz 1
arr = [1, 5, 3];
i = 1;
arr[i++] = arr[i++];
arr; //=>?
what is the result and why?
Written by Davit Khaburdzania
Related protips
4 Responses
[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
·
mvasilkov is right. Results are browser dependent:
over 1 year ago
·
@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
·
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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Nodejs
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#