/

HTML / CSS / JavaScript Tutorial

JavaScript Operator: increment

[this page | pdf | back links]

In JavaScript, the ++ operator is the (unary) arithmetic operator for incrementing, i.e. adding one to the variable. There are technically two different increment operators, the prefix one, and the postfix one.

 

For example, if x is 8 then the statement y = ++x results in x being incremented to 9 and then y assigned this value (i.e. is assigned 9). However, the statement y = x++ involves y being assigned the value of x (i.e. 8) and x then being incremented to 9.

 

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