Home >Web Front-end >JS Tutorial >The difference between ++i and i++ in js
The increment operator i in JavaScript first increments the variable value and then returns the incremented value, while i first returns the variable value and then increments it. Usage scenarios: If you need to use the variable value before incrementing, use i; if you need to use the variable value after incrementing, use i.
The difference between i and i in JavaScript
In JavaScript, i
and i
are two increment operators, used to increase the value of variable i
by 1. The main difference is the timing of the increment operation.
i
(prefix increment)
i
. Example:
<code class="javascript">let i = 0; console.log(++i); // 输出:1</code>
i
(suffix increasing)
i
. i
. Example:
<code class="javascript">let i = 0; console.log(i++); // 输出:0 console.log(i); // 输出:1</code>
Summary
Operator | Time to perform increment operation | Return value |
---|---|---|
## i
| Before increment The value after increment | |
i
| After incrementThe original value before increment |
Usage scenarios
before incrementing it ##i
.
Use i
.
The above is the detailed content of The difference between ++i and i++ in js. For more information, please follow other related articles on the PHP Chinese website!