What is the difference between the postfix i++ and prefix ++i increment operators?

Both increment the variable value by 1. The difference is what they evaluate to.

The postfix increment operator evaluates to the value before it was incremented.

let i = 0
i++ // 0
// i === 1

The prefix increment operator evaluates to the value after it was incremented.

let i = 0
++i // 1
// i === 1

Leave a Reply

Your email address will not be published. Required fields are marked *

*