Operators > ++ (increment)

++ (increment)

Syntax

++expression
expression++

Arguments

expression A variable, number, element in an array, or property of an object.

Description

Operator; a pre-increment and post-increment unary operator that adds 1 to the expression. The pre-increment form of the operator (++expression) adds 1 to the expression and returns the result. The post-increment form of the operator (expression++) adds 1 to the expression and returns the initial value of the expression (the result prior to the addition).

The pre-increment form of the operator increments x to 2 (x + 1 = 2), and returns the result as y:

x = 1;
y = ++x

The post-increment form of the operator increments x to 2 (x + 1 = 2), and returns the original value (x = 1) as the result y:

x = 1;
y = x++;

Player

Flash 4 or later.

Example

The following example uses ++ as a pre-increment operator with a while statement.

i = 0
while(i++ < 5){
// this section will execute five times
}

The following example uses ++ as a pre-increment operator:

var a = [];
var i = 0;
while (i < 10) {
	a.push(++i);
}
trace(a.join());

This script prints the following:

1,2,3,4,5,6,7,8,9

The following example uses ++ as a post-increment operator:

var a = [];
var i = 0;
while (i < 10) {
a.push(i++);
	}
trace(a.join());

This script prints the following:

0,1,2,3,4,5,6,7,8,9