We use decrement operator to decrement the value of a variable by 1.
- Pre-decrement operation
- Post-decrement operation
Pre-decrement operator (- -a)
Pre-decrement operator, decrements self first by 1 then assigns the value.
var b=- -a;
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.
var b=a- -;
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.