Last Updated: February 25, 2016
·
1.541K
· bright-day

Javascript Quiz 1

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

what is the result and why?

4 Responses
Add your response

[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:

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

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
Did you know?

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