The Difference Between Postfix and Prefix Increment
The increment operator in JavaScript is used for incrementing a value by 1. It has two different syntaxes, but both of them are used for incrementing a value by 1. The question is: What they will be evaluated to?
i++
, called postfix increment will evaluate to the value before it was incremented.++i
, called the prefix increment will evaluate to the value after it was incremented.
Copied to clipboard!
let i = 0;
i++ // Will evaluate to 0
++i // Will evaluate to 1
You can also do the same thing with a decrement operator. The behavior is exactly the same.
Copied to clipboard!
let i = 0;
i-- // Will evaluate to 0
--i // Will evaluate to -1
Resources:
π More Webtips
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: