DEV Community

Cover image for Difference Between ++a And a++ In JavaScript?
yns
yns

Posted on

Difference Between ++a And a++ In JavaScript?

The ++ is responsible of the increment of the number value by 1 . But have you wondered what is the difference between putting it before or after?

  • ++a : returns the value after the increment.

  • a++ : returns the value before the increment.

Let's take an example:

let a=0;
a++;
++a;

the output will be:
output->0
1

Top comments (1)

Collapse
 
tracygjg profile image
Tracy Gilmore

Hi Yns, This is a feature of many languages that derive their implementation from C.
I think a better example would be:

let count = 0;
console.log(++count); // logs 1
console.log(count++); // logs 1
console.log(count); // logs 2
Enter fullscreen mode Exit fullscreen mode