educative.io

Educative

What's the difference?

what’s the different using “++number” and “number++” in javascript?

1 Like

++a increments and then uses the variable.
a++ uses and then increments the variable.

For example:

a=1, b=1
System.out.println(a++); //You will see 1
System.out.println(a); //You will see 2
System.out.println(++b); //You will see 2
System.out.println(b); //You will see 2

++number and number++ both increment the value of number by 1 but in a different way. If ++ precedes the variable, it is called pre-increment operator and it comes after a variable, it is called post-increment operator.

Increment in java is performed in two ways,

  1. Post-Increment (number++): we use number++ in our statement if we want to use the current value, and then we want to increment the value of number by 1.

  2. Pre-Increment(++number) : We use ++number in our statement if we want to increase the number’s value by 1 and then use it in our statement.