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

JavascriptWeb DevelopmentFront End Technology

++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>
raja
Published on 19-Jan-2018 09:35:10
Advertisements