In JavaScript, "++
" is known as the increment operator. It increments the value of the operand by one and returns the value. It can be used in the following two ways:
// prefix increment ++i; // postfix increment i++;
The main difference between the two is that:
- Prefix increment (
++i
) increments, and returns the new, incremented value; - Postfix increment (
i++
) increments, but returns the old value (i.e. the value before the increment).
To illustrate this difference, you can consider the following example:
let i = 0; console.log(++i); // 1 console.log(i); // 1
let i = 0; console.log(i++); // 0 console.log(i); // 1
Since in either case the operand value is incremented, you may use either one in the increment part of a for
loop.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.