> ## 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.

# JavaScript Subtraction

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.

```javascript theme={"dark"}
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.

```javascript theme={"dark"}
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.

```javascript theme={"dark"}
-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.

```javascript theme={"dark"}
-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()`.

```javascript theme={"dark"}
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.

```javascript theme={"dark"}
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`).

```javascript theme={"dark"}
Infinity - 5;               // Infinity
Infinity - Infinity;        // NaN
-Infinity - Infinity;       // -Infinity
0 - 0;                      // 0
-0;                         // -0 (Unary negation of 0 yields negative zero)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor JavaScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
