What is the difference between a++ and ++a in JavaScript?

++a returns the value of an after it has been incremented. It is a pre-increment operator since ++ comes before the operand.

a++ returns the value of a before incrementing. It is a post-increment operator since ++ comes after the operand.


You can try to run the following code to learn the difference between i++ and ++i −

          var a =10;
          var b =20;
          //pre-increment operator
          a = ++a;
          document.write("++a = "+a);            
          //post-increment operator
          b = b++;
          document.write("<br> b++ = "+b);

Updated on: 13-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started