Skip to main content
The - operator in JavaScript functions as both a binary subtraction operator and a unary negation operator. In both contexts, it performs implicit type coercion, forcing non-numeric operands to resolve to primitive numbers (or BigInts) before evaluating the mathematical expression.

Binary Subtraction

As a binary operator, - computes the difference between two operands.
Evaluation Mechanics:
  1. Both operands are evaluated.
  2. JavaScript applies the abstract ToNumeric operation to both operands.
  3. If the operands are of different numeric types (e.g., Number and BigInt), a TypeError is thrown.
  4. The right operand is subtracted from the left operand based on their resolved numeric type:
    • If the operands are of type Number, the operation is performed according to IEEE 754 double-precision floating-point arithmetic.
    • If the operands are of type BigInt, the operation is performed using arbitrary-precision mathematical integer arithmetic.
Implicit Coercion Behavior: Unlike the + operator, which is overloaded for string concatenation, the binary - operator strictly enforces numeric evaluation.

Unary Negation

As a unary operator, - precedes a single operand. It coerces the operand to a numeric value and inverts its algebraic sign.
Evaluation Mechanics:
  1. The operand is evaluated.
  2. The abstract ToNumeric operation is applied.
  3. If the resulting value is NaN, the result remains NaN.
  4. Otherwise, the sign of the numeric value is inverted.

Object Coercion (ToPrimitive)

When the - operator is applied to an Object, JavaScript attempts to resolve a primitive value by invoking the object’s internal [[ToPrimitive]] method, prioritizing the valueOf() method before falling back to toString().

BigInt Mechanics

The - operator fully supports BigInt primitives for both binary subtraction and unary negation. However, JavaScript prohibits mixing BigInt and Number types to prevent precision loss.

IEEE 754 Edge Cases

Because JavaScript Number types are floating-point values, the - operator adheres to specific rules regarding Infinity, NaN, and signed zeros (+0 and -0).
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More