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 - 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.
LeftOperand - RightOperand
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.
10 - 3;         // 7
10 - "4";       // 6   (String "4" coerced to Number 4)
10 - true;      // 9   (Boolean true coerced to Number 1)
10 - false;     // 10  (Boolean false coerced to Number 0)
10 - null;      // 10  (null coerced to Number 0)
10 - undefined; // NaN (undefined coerced to NaN)

Unary Negation

As a unary operator, - precedes a single operand. It coerces the operand to a numeric value and inverts its algebraic sign.
-Operand
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.
-5;             // -5
-"42";          // -42 (String coerced to Number, then negated)
-true;          // -1  (Boolean coerced to Number, then negated)
-"text";        // NaN (Unparseable string coerces to NaN)

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().
10 - { valueOf: () => 3 }; // 7
10 - {};                   // NaN ({} coerces to "[object Object]", then to NaN)
10 - [];                   // 10  ([] coerces to "", then to 0)
10 - [2];                  // 8   ([2] coerces to "2", then to 2)
10 - new Date(0);          // 10  (Date coerces to its epoch timestamp, 0)

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.
20n - 5n;       // 15n
-42n;           // -42n
20n - 5;        // TypeError: Cannot mix BigInt and other types

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).
Infinity - 5;               // Infinity
Infinity - Infinity;        // NaN
-Infinity - Infinity;       // -Infinity
0 - 0;                      // 0
-0;                         // -0 (Unary negation of 0 yields negative zero)
Master JavaScript with Deep Grasping Methodology!Learn More