Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The increment operator (++) is a unary mutator operator that adds one (1) to its operand and evaluates to a numeric value. It requires an l-value (such as a variable, array element, or object property) as its operand. If the operand is not already a numeric type, the operator applies the internal ToNumeric abstract operation to coerce it into a Number or BigInt before performing the addition. The operator has two distinct syntactic forms that dictate the order of evaluation and mutation: postfix and prefix.
// Postfix syntax
operand++

// Prefix syntax
++operand

Postfix Increment (x++)

In the postfix position, the operator evaluates and returns the current value of the operand before the increment operation occurs. The mutation happens in memory, but the expression itself resolves to the unmutated value.
let x = 10;
let y = x++; 

console.log(y); // 10 (Value returned before increment)
console.log(x); // 11 (Variable mutated in memory)

Prefix Increment (++x)

In the prefix position, the operator increments the operand in memory first, and then the expression evaluates to the newly incremented value.
let a = 10;
let b = ++a; 

console.log(b); // 11 (Value returned after increment)
console.log(a); // 11 (Variable mutated in memory)

Implicit Type Coercion and ToNumeric

Unlike the binary addition operator (+), which can perform string concatenation, the ++ operator strictly performs mathematical addition. By applying the ToNumeric abstract operation, the operator handles different data types as follows:
  • BigInt: If the operand is a BigInt, it is incremented by 1n. It is not coerced to a Number, and its type remains "bigint".
  • Valid Coercible Types: If the operand is a numeric String, Boolean, or Null, it is coerced into a Number and incremented by 1. Because JavaScript variables are dynamically typed, this simply reassigns the variable to a new value of type Number.
  • Invalid Numeric Strings and undefined: If the operand is undefined or a string that cannot be parsed into a valid number (e.g., "hello"), the ToNumeric operation evaluates to NaN. Incrementing NaN results in NaN.
  • Symbols: Symbols cannot be coerced into numeric values. Applying the increment operator to a Symbol will throw a TypeError.
// Numeric String coerced to Number
let str = "5";
str++; 
console.log(str); // 6 (Variable reassigned to a Number value)

// Non-numeric String coerces to NaN
let text = "hello";
text++;
console.log(text); // NaN

// undefined coerces to NaN
let undef;
undef++;
console.log(undef); // NaN

// BigInt remains BigInt
let big = 10n;
big++;
console.log(big); // 11n (Type is still BigInt)

// Boolean coerced to Number
let bool = true;
++bool;
console.log(bool); // 2 (true coerced to 1, then incremented)

// Symbol throws TypeError
let sym = Symbol("id");
sym++; // TypeError: Cannot convert a Symbol value to a number

Invalid Operands

Because the ++ operator must mutate its operand, it cannot be applied to r-values such as literals or the evaluated results of expressions. Attempting to do so results in a SyntaxError during the parsing phase.
5++; // SyntaxError: Invalid left-hand side expression in postfix operation
(x + y)++; // SyntaxError: Invalid left-hand side expression in postfix operation
Master JavaScript with Deep Grasping Methodology!Learn More