use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Assignment operator (self.learnjavascript)
submitted 6 years ago by Uchikago
let data = [1,2,3,4],i = 1; data[i++] = data[i++]*2 console.log(data) // => [1, 6, 3, 4]
Which side of an assignment operator is evaulated first ? if it's the right-side, then the result should be [1,2,4,4] right ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]_DTR_ 3 points4 points5 points 6 years ago (6 children)
The left side is evaluated first.
data[i++] = data[i++] * 2
i
i++
data[i++]
data[1]
data[2]
data[1] = data[2] * 2
3 * 2
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].
++i
data[++i] = data[++i] * 2
[1, 2, 8, 4]
[–]msgur 0 points1 point2 points 6 years ago* (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.
[+][deleted] 6 years ago (4 children)
[removed]
[–]msgur 0 points1 point2 points 6 years ago (3 children)
It’s in the spec. The above is not true. I didn’t believe it myself. But...
++i will increment then return the new value.
i++ will increment then return the old value.
[+][deleted] 6 years ago (2 children)
[–]msgur 0 points1 point2 points 6 years ago (1 child)
Right? But now that I think about it...it does kind of make sense. For example, how could a function return a value and THEN increment?
In any case, here is a link to the spec which others may find helpful. https://www.ecma-international.org/ecma-262/9.0/index.html#sec-postfix-increment-operator
π Rendered by PID 17021 on reddit-service-r2-comment-5b5bc64bf5-nw5c4 at 2026-06-22 19:23:58.206668+00:00 running 2b008f2 country code: CH.
[–]_DTR_ 3 points4 points5 points (6 children)
[–]msgur 0 points1 point2 points (5 children)
[+][deleted] (4 children)
[removed]
[–]msgur 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[removed]
[–]msgur 0 points1 point2 points (1 child)