The Difference Between Postfix and Prefix Increment

The Difference Between Postfix and Prefix Increment

Ferenc Almasi β€’ 2020 December 11 β€’ Read time 1 min read
  • twitter
  • facebook
JavaScript

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
increment.js

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
increment.js
The difference between postfix and prefix increment in JavaScript
If you would like to see more Webtips, follow @flowforfrank

50 JavaScript Interview Questions

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.