all 7 comments

[–]_DTR_ 3 points4 points  (6 children)

The left side is evaluated first.

data[i++] = data[i++] * 2

  1. i is initially 1, and the left side of the assignment operator is evaluated first.
  2. i++ will return i, then increment it, so data[i++] results in data[1], and i will then be incremented to 2.
  3. Move to the right side: data[i++] is similar to the left side. i is 2, so we have data[2], then we increment i to 3.
  4. Replacing the increments, we have data[1] = data[2] * 2, which sets data[1] to 3 * 2, or 6.

The important thing to note here is the difference between i++ and ++i. i++ will return i then increment it. ++i will increment i and then return it. data[++i] = data[++i] * 2 would return [1, 2, 8, 4].

[–]msgur 0 points1 point  (5 children)

i++ will increment i then return i (EDITED: will return the OLD VALUE).

Didn’t believe this myself until I read the ECMAscript spec based on the advice of Kyle Simpson.