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.
mvasilkov is right. Results are browser dependent:
@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]
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) */
arrHave a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside