Most of new Javascript developers often get confused with these two. So let's find out what really is the difference between them.

++x

let x = 1;

console.log(++x); // 2

You'll notice that ++x returns 2, this is because JavaScript increments x (x becomes 2), then returns it.

So the value is 2 when you print it.

  1. Increment x - 2
  2. Return x - 2

x++

let x = 1;

console.log(x++); // 1

Now,  why does x++ returns 1?

It's because JavaScript returns x (with value 1) then increments it afterwards.

  1. Return x - 1
  2. Increment x - 2

So if you print x again you'll get 2.

console.log(x++); // 1
console.log(x);   // 2

Simple right?