JavaScript Decrement Operator

The JavaScript decrement operator subtracts one from it’s operand. The decrement operator can be used either as a prefix or a postfix:

var x = 1;
y = --x; //Prefix; y is 0
document.write(y);

var x = 1;
y = x--; //Postfix; y is 1
document.write(y);

As seen in the previous example, when the decrement operator is applied to a variable prior to the variable evaluation, the variable has the decremented value when evaluated. If the decrement operator is applied to a variable after the variable evaluation, the variable is evaluated with the value it contains before it is decremented.

The decrement property can be applied on a variable, an array element or a property of an object.

Related articles:
JavaScript Increment Operator

Leave a Reply