Archive for July 3, 2007

JavaScript Increment Operator

Posted in JavaScript, Software, Web, internet, programming on July 3, 2007 by Joey

The JavaScript increment operator adds one to it’s operand. The increment operator can be used either as a prefix or a postfix:

var x = 1;
y = ++x; //Prefix; y is 2
document.write(y);

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

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

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

Related articles:
JavaScript Decrement Operator