/

HTML / CSS / JavaScript Tutorial

JavaScript Operator: decrement

[this page | pdf | back links]

In JavaScript, the -- operator is the (unary) arithmetic operator for decrementing, i.e. subtracting one from the variable.

 

There are technically two different decrement operators, the prefix one, and the postfix one. For example, if x is 8 then the statement y = --x results in x being decremented to 7 and then y assigned this value (i.e. is assigned 7). However, the statement y = x-- involves y being assigned the value of x (i.e. 8) and x then being decremented to 7.

 

For code clarity, some commentators suggest using the statements x = x - 1; y = x; instead of y = --x and y = x; x = x - 1; instead of y = x--.

 


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Operators


Desktop view | Switch to Mobile