Timeline for Javascript increment while assigning
Current License: CC BY-SA 3.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 29, 2015 at 8:23 | history | edited | Cerbrus | CC BY-SA 3.0 |
added 6 characters in body
|
| Nov 26, 2012 at 15:01 | vote | accept | Community Bot | moved from User.Id=1726343 by developer User.Id=18937026 | |
| Nov 26, 2012 at 13:35 | comment | added | Cerbrus | @EliasVanOotegem That's what I was trying to say, yea, the comment was in reply to Asad | |
| Nov 26, 2012 at 13:27 | history | edited | Cerbrus | CC BY-SA 3.0 |
deleted 6 characters in body
|
| Nov 26, 2012 at 13:26 | comment | added | Cerbrus |
But if the assignment didn't take place, v = ++v + ++v; would return 2, yet it returns 3.
|
|
| Nov 26, 2012 at 13:24 | comment | added | Elias Van Ootegem |
@Asad: the assignment always takes place, regardless of where it is written: it's an expression all in itself. JS, like almost any other language, will first resolve all expressions within a statement, from the inside out. the difference is: ++x resolves to the incremented value, whereas x++ resolves to the value of x prior to it being incremented, just try x=0, then x++ and finally x in your console: 0, 0(<--old value) and 1 will be the result. then try ++x, it'll show 2 right away: the returned value is the new value when the increment operator comes first
|
|
| Nov 26, 2012 at 13:03 | comment | added | user1726343 |
I am not saying ++x resolves to x+1. I'm saying it resolves to x=x+1, but when it is on the right hand side of an operation the assignment does not take place.
|
|
| Nov 26, 2012 at 12:59 | history | edited | Cerbrus | CC BY-SA 3.0 |
added 161 characters in body
|
| Nov 26, 2012 at 12:52 | history | answered | Cerbrus | CC BY-SA 3.0 |