11.3.1 Postfix Increment Operator

2010-05-16

PostfixExpression : LeftHandSideExpression [no LineTerminator here] ++

Code: (Meta Ecma)
function (LeftHandSideExpression ++) {
var lhs = evaluate(LeftHandSideExpression);
if (Type(lhs) == 'reference' && IsStrictReference(lhs) && Type(GetBase(lhs)) == 'environmentRecord' && (GetReferencedName(lhs) == 'eval' || GetReferencedName(lhs) == 'arguments')) throw TypeError;
var oldValue = ToNumber(GetValue(lhs));
var newValue = oldValue + 1; // use rules in 11.6.3
PutValue(lhs, newValue);
return oldValue;
}

Note that something interesting happens here, especially in regard to the Prefix operators. Whatever variable you incremented will get incremented (even if it's an object because they explicitly use the + operator). But a new object containing the old value (as a number) will be returned! See this example:

Code: (Ecma)
var x = {valueOf: function(){ return 5; }};
alert(x); // object
alert(x++); // 5
alert(x); // 6 (object disappears)