JavaScript self-increment, decrement operator, and expression syntax
var
i
++;
var
--Declaring variables
i
--Variable name
++
--Self-increment operator
JavaScript self-increment, decrement operator and expression
JavaScript self-increment, decrement operator and expression (I initial value is 6)
Operator |
++i |
--i |
i++ |
i-- |
Name |
Pre-increment operator |
Pre-decrement operator |
Post-increment operator |
Post-decrement operator |
An expression |
++i |
-I. |
i++ |
i-- |
Example |
++i; |
I.; |
i++; |
i--; |
Results of I |
7 |
5 |
7 |
5 |
An interesting example |
++i Alert (i) Alert (++i) Alert (i) |
-I. Alert (i) Alert (-I.) Alert (i) |
i++ Alert (i) Alert (i++) Alert (i) |
i-- Alert (i) Alert (i--) Alert (i) |
Results |
7 8 8 |
5 4 4 |
7 7 8 |
5 5 4 |
Example explanation
The pre-increment operator is essentially different from the post-increment operator, where the same point is 1 for itself, and the difference is that the former increment operator is preceded by 1, then the operand value, and the increment operator is the value of the operand first, plus 1. For example:
var
a
;
var
i
=
6
;
//
(前加加)i加1后,i等于7,并将i值赋予a,于是a等于7
a
=++
i
;
document
.
write
(
i
)
;
document
.
write
(
a
)
;
i
=
6
;
//
(后加加)将i值赋予a,于是a等于6,最后i加1,i等于7
a
=
i
++;
document
.
write
(
i
)
;
document
.
write
(
a
)
;
Results
7776
Original link: http://www.cnblogs.com/leejersey/archive/2011/12/12/2284468.html
JavaScript self-increment, decrement operator