Lodash _.subtract() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Math

Definition

Subtract two numbers. Part of Lodash Math since 4.0.0 (stable — not deprecated).

Warning: If one argument is undefined, Lodash returns the other value unchanged — so _.subtract(undefined, 5) is 5, not -5. Prefer null (→ 0) when a missing minuend should mean zero.

Syntax

Call _.subtract. Start with this line.

Lodash
_.subtract(minuend, subtrahend)
CallWhat it does
_.subtract(minuend, subtrahend)Primary signature.
_.subtract(a, b)Run the math helper on numbers or a collection.

Prefer import subtract from "lodash/subtract" when you want a smaller bundle.

Library declarations
Lodash
_.subtract(minuend, subtrahend)

Parameters

ParameterDescription
minuendOptional. First number (left side of −)
subtrahendOptional. Second number (right side of −)

Return Value

Return ValueDescription
*Difference, or short-circuit / default 0

Lodash Examples

Example 1 — Official Subtraction

Subtract two numbers and get the difference.

Lodash
_.subtract(6, 4);
// → 2
Try It Yourself

How It Works

  1. Input. Official sample: minuend 6, subtrahend 4.
  2. Op. Both defined → numeric subtraction.
  3. Result. 6 − 4 = 2.
  4. Same as. Native 6 - 4 for finite numbers.

Example 2 — Order Matters

Swapping arguments flips the sign.

Lodash
_.subtract(6, 4);
// → 2

_.subtract(4, 6);
// → -2
Try It Yourself

How It Works

  1. First call. Start at 6, remove 4 → 2.
  2. Second call. Start at 4, remove 6 → -2.
  3. Not commutative. Unlike _.add / _.multiply.
  4. Tip. Keep “start − amount” order clear in APIs.

Example 3 — undefined vs null

Missing values behave differently — know the short-circuit.

Lodash
_.subtract(undefined, 5);
// → 5   // returns the other arg

_.subtract(null, 5);
// → -5  // null → 0, then 0 − 5
Try It Yourself

How It Works

  1. undefined. Factory returns the defined operand as-is.
  2. null. Coerces to 0, then subtracts normally.
  3. Choose. Use null when “empty” should mean zero.
  4. Both missing. _.subtract() → 0.

Example 4 — Numeric Strings

Numeric strings coerce; non-numeric strings become NaN.

Lodash
_.subtract('10', '3');
// → 7

_.subtract(10, '3');
// → 7
Try It Yourself

How It Works

  1. Coerce. toNumber turns numeric strings into numbers.
  2. Subtract. 10 − 3 = 7.
  3. vs add. _.add(10, '3') can concatenate — subtract always numeric.
  4. Garbage. 'abc' → NaN.

Example 5 — subtract vs add

Same operands, opposite operations.

Lodash
_.subtract(6, 4);  // → 2
_.add(6, 4);       // → 10
Try It Yourself

How It Works

  1. subtract. Difference → 2.
  2. add. Sum → 10.
  3. Family. Same Math helpers style as multiply/divide.
  4. Pick. Use the op that matches your formula.

Browser & Runtime Support

_.subtract is a Lodash Math helper since 4.0.0. It uses native subtraction plus Lodash number coercion. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.0.0+

_.subtract()

Available in [email protected] full builds and the lodash/subtract modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.subtract Lodash 4.0.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import subtract from "lodash/subtract" for smaller bundles. For simple scripts, native - is fine; Lodash shines in functional pipelines and consistent Math helpers.

Frequently Asked Questions

_.subtract(minuend, subtrahend) returns minuend − subtrahend. Official docs: _.subtract(6, 4) → 2.
Yes. Subtraction is not commutative. _.subtract(6, 4) → 2 but _.subtract(4, 6) → -2.
_.subtract() with both undefined returns 0 — the additive identity used by Lodash’s createMathOperation for add/subtract.
When one side is undefined, Lodash returns the other operand unchanged. That is handy for add/multiply, but for subtract it is not the same as 0 − 5. Prefer null (coerces to 0) or guard missing values.
No. _.subtract(0.3, 0.2) matches native 0.3 − 0.2. Use integer cents or a decimal library for money.
Use import subtract from "lodash/subtract" or require("lodash/subtract") for tree-shaking. Full build: import _ from "lodash" then _.subtract.

Next: _.sum()

Sum all numbers in a collection with Lodash.

_.sum() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful