References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Subtraction - operator

Operator JavaScript All modern browsers Updated
Quick answer

The subtraction operator - subtracts one number from another: 10 - 3 is 7. Unlike +, it always does math — it coerces string operands to numbers, so "10" - 3 is 7. As a unary operator, -x negates, and -"5" converts a string to the number -5.

Overview

- subtracts the right number from the left: 10 - 3 is 7. Simple enough — but there's a useful contrast with addition. Where + is overloaded to also concatenate strings, - is purely arithmetic. It coerces string operands to numbers first, so "10" - 3 is 7 (not a string), and "10" - "4" is 6. That's why people sometimes use - to spot coercion behavior — only + joins strings.

As a unary operator (one operand), -x negates a number, flipping its sign. And like unary plus, unary minus converts to a number: -"5" is -5 and -("3") coerces the string. If a value can't be converted, the result is NaN"abc" - 1 is NaN.

It sits with the other arithmetic operators — +, *, / and % — and follows standard precedence (multiplication and division before subtraction). There's a compound form, -=, that subtracts and reassigns in one step (x -= 2), and floating-point precision applies as with all number math.

Syntax

a - b

10 - 3       // 7
"10" - 3     // 7   (string coerced to number)
"abc" - 1    // NaN
-x           // unary negation
-"5"         // -5  (converts to number)
x -= 2;      // subtract and reassign

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const total = 100;
  const spent = '35'; // a string from a form

  document.getElementById('out').textContent =
    'remaining: ' + (total - spent) + '\n' +
    'negate 8:  ' + (-8) + '\n' +
    '"abc" - 1: ' + ('abc' - 1); // remaining: 65 / negate 8: -8 / NaN
</script>

Best practices

  • Remember - always does math — it coerces strings to numbers, unlike +.
  • Use unary minus (or unary plus) to convert a string to a number quickly.
  • Validate that operands are real numbers; non-numeric strings produce NaN.
  • Use -= to subtract and reassign in one step.

Frequently asked questions

Why does "10" - 3 work but "10" + 3 give "103"?
Only + does string concatenation. The - operator always coerces strings to numbers, so "10" - 3 is 7.
What does unary minus do?
It negates a number (-x flips the sign) and converts a value to a number, so -"5" is -5.
Why do I get NaN from subtraction?
Because an operand couldn't be converted to a number, e.g. "abc" - 1 is NaN.
What does -= do?
It subtracts and reassigns: x -= 2 is shorthand for x = x - 2.