So the following two functions print out the same exact results.
console.log("i++");
for (i=1; i<=3; i++) {
console.log(i); // 1, 2
}
console.log("++i");
for (i=1; i<=3; ++i) {
console.log(i); // 1, 2
}
This very counter intuitive as I'm specifically asking to post-increment one and per-increment the other. It would be very desirable behavior to increment the value before the run inside the for loop. Is this behavior consistent?, Is this javascript specific or is this the standard behavior across programming languages that use ++i, i++ syntax and for loops?
The third expression in the for
loop header is evaluated after each iteration. Thus:
i
is initialized to 1
i <= 3
, is evaluated (and found to be true
) i++
or ++i
happens Except for the minor syntax differences, that's exactly what would have happened in a C program in 1976.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.