+ 3

I don't understand an answer to a question amongst the Javascript challenges. Hoping someone can explain?

What is the output of this code? var x = 0; var arr = [4,9,2]; arr[x] = (++x) -1; document.write(arr[x]); Apparently the answer is 9. I don't understand why it's not 4.

22nd Aug 2023, 7:23 PM
💙 Aimee
💙 Aimee - avatar
2 Answers
+ 5
The position 0 got replaced by 0 meanwhile, x was incremented so x is 1, because of the ++x then it writes what is at the position 1 which is 9 I hope this helps :)
22nd Aug 2023, 7:35 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
Thank you. For some reason I just couldn't make sense of the code :) Looks like I need to refresh my memory on arrays.
22nd Aug 2023, 8:14 PM
💙 Aimee
💙 Aimee - avatar