Possible Duplicate:
++someVariable Vs. someVariable++ in Javascript
I know you can add one to a variable simply by doing i++
(assuming i is your variable). This can best be seen when iterating through an array or using it in a "for" statement. After finding some code to use online, I noticed that the for statement used ++i
(as apposed to i++
).
I was wondering if there was any significant difference or if the two are even handled any differently.
The Complete Full-Stack JavaScript Course! ++a returns the value of an after it has been incremented. It is a pre-increment operator since ++ comes before the operand. a++ returns the value of a before incrementing. It is a post-increment operator since ++ comes after the operand.
JavaScript has an even more succinct syntax to increment a number by 1. The increment operator ( ++ ) increments its operand by 1 ; that is, it adds 1 to the existing value. There's a corresponding decrement operator ( -- ) that decrements a variable's value by 1 . That is, it subtracts 1 from the value.
The Set object lets you store unique values of any type, whether primitive values or object references. you are passing new object not reference so it is allowing to add duplicate.
Yes there is a big difference.
var i = 0;
var c = i++; //c = 0, i = 1
c = ++i; //c = 2, i = 2
//to make things more confusing:
c = ++c + c++; //c = 6
//but:
c = c++ + c++; //c = 13
And here is a fiddle to put it all together: http://jsfiddle.net/maniator/ZcKSF/
The value of ++i
is i + 1
and the value of i++
is just i
. After either has evaluated, i
is i + 1
. It's a difference in timing, which is why they're often called 'pre-increment' and 'post-increment'. In a for loop, it rarely matters, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With