What's the Difference Between i++ and ++i in JavaScript?

Differences between i++ and ++i in JavaScript

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

// postfix increment

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.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.