Decrement operator in Javascript

Javascript provides decrement operation. To perform decrement operation we use decrement operator. Decrement operators are helpful in lots of situation. Suppose we have allowed user to perform a task for a limited number of times. By using decrement operator, we can track how many attempts left for the user to perform the task.

We use decrement operator to decrement the value of a variable by 1. 

There are two types of decrement operator exist in javascript:

Pre-decrement operator (- -a)

Pre-decrement operator, decrements self first by 1 then assigns the value.

Example:

var a=2;

var b=- -a;

alert(a);

alert(b);

The above code would print 1, 1

Because, a decremented self first by 1 then assigned to the variable b.

Post-decrement operator(a- -)

Post-decrement operator, assigns self first then decrements self by 1.

Example:

var a=2;

var b=a- -;

alert(a);

alert(b);

The above code would print 1, 2

Because, the value of a is assigned first to the variable b then the value of a is decremented by 1.

So we have to careful while using pre-decrement operator and post-decrement operator.