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.

Example

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

<html>
   <body>  
      <script>
          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);
      </script>
   </body>
</html>

Updated on: 13-Jun-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements