How to Subtract in JavaScript

To subtract in JavaScript, put the minus operator between two values: 10 - 4 returns 6. That works on literal numbers, variables and expressions, in any modern browser console or in Node.js. This tutorial covers plain subtraction, the -= and -- shortcuts, subtracting values from form fields and arrays, finding the difference between two dates, and the decimal and type problems that produce surprising answers.

What you need before you start

You only need somewhere to run code. The quickest option is your browser’s JavaScript console, a panel in the developer tools where you can type a line and see the result immediately. A few terms show up below:

  • Operand: a value on either side of an operator. In a - b, a and b are the operands.
  • Coercion: JavaScript automatically converting a value to another type, such as turning the string "5" into the number 5.
  • NaN: short for “Not a Number,” the special number value you get when a calculation has no meaningful numeric answer.
  • BigInt: a separate numeric type for very large whole numbers, written with an n suffix, as in 2n.

Subtract two numbers with the minus operator

  1. Open the developer tools in your browser. On Windows and Linux press F12 or Ctrl+Shift+I; on a Mac press Cmd+Option+I.
  2. Click the Console tab (Firefox calls this tool the Web Console).
  3. Type 15 - 9 and press Enter. The console prints 6.
  4. To keep the answer, store it in a variable: const difference = 15 - 9;
  5. Print it with console.log(difference); to see 6 again.

Order matters. 9 - 15 gives -6, because the right-hand value is taken away from the left-hand one. You can chain several subtractions, and they run left to right, so 20 - 5 - 3 is (20 - 5) - 3, which is 12. Multiplication and division happen before subtraction: 10 - 2 * 3 returns 4, not 24. Add parentheses when you want a different order, as in (10 - 2) * 3.

The full operator rules are on the MDN reference for the subtraction operator.

Shortcuts that subtract from a variable

Subtraction assignment (-=)

When you want to reduce a value that is already stored, x -= y does the same job as x = x - y. It is handy for running totals such as a balance or a countdown. The variable must be declared with let (not const), because its value changes.

Decrement (–)

To subtract exactly 1, use --. The position changes what the expression hands back. With let x = 3;, writing const y = x--; leaves x at 2 but sets y to 3 (the old value). Writing const y = --x; instead sets both to 2. The operand must be a variable or object property, so 5-- is a syntax error.

A reusable function

If the same calculation appears in several places, wrap it in a function such as const subtract = (a, b) => a - b;. Calling subtract(50, 8) returns 42. Choose the plain operator for one-off math, -= for updating a total, -- for counters, and a function when the logic needs a name or extra checks.

Worked example: tracking a budget

Priya starts the month with a 500 dollar budget, then pays for groceries and a phone bill. Alex and Jordan each owe her a share of a dinner, and she wants to know how much each still owes after partial payments.

let budget = 500;
budget -= 120;    // groceries
budget -= 45.5;   // phone bill
console.log(budget);

const owed = [30, 25];   // Alex, Jordan
const paid = [12, 25];
const stillOwed = owed.map((amount, i) => amount - paid[i]);
console.log(stillOwed);

Output:

334.5
[ 18, 0 ]

The map() method builds a new array by running the arrow function on each element, and its second argument (i) is the element’s position, which lets you pair each amount with the matching payment. The original arrays are left unchanged. Some consoles print arrays without the spaces, as [18, 0].

Subtracting values from a form or text

A web page input’s value property is always a string, even for type="number" fields. Subtraction coerces strings to numbers, so "10" - "4" still returns 6. Trouble starts when the same code also adds: + joins strings instead of adding them, so "10" + "4" - 1 returns 103. Convert first to stay safe:

  1. For number inputs, read input.valueAsNumber, which returns a number, or NaN if the field is empty or invalid.
  2. For text such as "12px", use parseFloat(), which reads the leading number and ignores the rest, giving 12.
  3. Check the result with Number.isNaN(value) before you subtract, and show a message if it returns true.

Finding the difference between two dates

Subtracting one Date object from another converts both to timestamps and returns the gap in milliseconds. Divide by 86,400,000 (1000 × 60 × 60 × 24) to get days:

const start = new Date(2026, 0, 1);   // January 1 (months start at 0)
const end = new Date(2026, 0, 15);    // January 15
console.log((end - start) / 86400000); // 14

Writing end.getTime() - start.getTime() produces the same number and makes your intent clearer to other readers. If you are doing the same job in a spreadsheet, the approach differs; see subtracting dates in Excel for the cell-based version.

Why the answer looks wrong

What you seeCauseFix
0.3 - 0.1 gives 0.19999999999999998Decimals are stored in binary and many cannot be represented exactlyRound for display with .toFixed(2), or work in whole cents
NaNOne operand could not become a number, such as "hello" or undefinedConvert and check inputs with Number.isNaN()
TypeError: Cannot mix BigInt and other typesA BigInt and a regular number in the same subtractionConvert one side: 2n - BigInt(1) or Number(2n) - 1
A negative result you did not expectOperands in the wrong orderSwap them, or use Math.abs(a - b) if only the size of the gap matters
5 - true gives 4Booleans coerce to 1 and 0Make sure the variable holds the number you think it does

Be careful with toFixed(): it returns a string, so wrap it in Number() if you plan to keep calculating, as in Number((0.3 - 0.1).toFixed(2)), which gives 0.2. For a negative literal, add parentheses, (-2.34).toFixed(1), because the method call binds before the minus sign.

Checking and correcting a result

Log both the value and its type: console.log(result, typeof result) should print a number followed by number. A quick reverse check also helps: adding the subtracted amount back (result + b) should return the original value. Never compare decimal results with ===; test whether the gap is tiny instead, for example Math.abs(a - b) < Number.EPSILON for values near 1, or a tolerance such as 0.01 for money. If a subtraction went into the wrong variable, there is no undo; re-run the code from the original values, which is why keeping inputs in their own const variables and writing results to new ones is a good habit.

Other things people ask

How do I subtract a negative number?

Write it with a space or parentheses: 5 - -3 or 5 - (-3) both return 8. Unary minus is applied before the subtraction.

Is there a built-in subtract function?

No. JavaScript has no Math.subtract(); the - operator is the standard tool, and you can write your own function if you want one.

Does subtraction work the same in other languages?

The minus sign is nearly universal, but type rules differ. Python refuses to subtract a string from a number rather than converting it, as covered in how subtraction works in Python, and databases have their own date math, explained in subtracting in SQL queries.

Next, try rewriting the budget example with your own numbers, then add a Number.isNaN() check so bad input never reaches your subtraction.

Matthew Burleigh

Matthew Burleigh has been writing technology tutorials online since 2008. He holds Bachelor's and Master's degrees in Computer Science from Arizona State University and has worked in IT since 2003, from small-business IT support to IT management and consulting.