Lodash _.divide() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.divide(dividend, divisor) wraps native / with _.toNumber / string handling from createMathOperation.
  • The multiplicative identity rules: both undefined1; one side missing → return the other operand unchanged.
  • That divide-by-zero yields ±Infinity or NaN exactly like JavaScript—lodash does not throw or “sanitize” it.
  • A practical split: proportional shares with _.divide plus _.round when you need display precision.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

For the symmetric factory story see _.multiply() in the official docs; for rounding quotients pair with _.ceil() or _.round from the same docs, or open the official _.divide docs.

  • IEEE-754 division: 10 / 0 is Infinity; 0 / 0 is NaN.
  • createMathOperation: same helper as _.add and _.multiply—compare defaults 0 vs 1.

Overview

At its core, _.divide(a, b) is a / b after the same coercion pipeline as _.multiply: numeric operands pass through baseToNumber; if either operand is a string, both stringify first, then JavaScript’s division rules apply. The only lodash-specific behavior is the undefined short-circuiting inherited from createMathOperation(..., 1).

Native quotient

Same Infinity, -Infinity, and NaN outcomes as /.

Nullish shortcuts

Designed for reducers and sparse math, not semantic “division with missing operands.”

Pairwise only

Two arguments—chain _.divide(_.divide(a, b), c) or fold manually for more divisors.

Syntax

javascript
_.divide(dividend, divisor)
  • dividend: numerator (first operand).
  • divisor: denominator (second operand).
  • Returns: quotient dividend / divisor after coercion, or one of the nullish shortcuts documented below.
1

Lodash docs baseline

The single documented example: six divided by four.

javascript
import divide from "lodash/divide";

const q = divide(6, 4);
console.log(q);
// => 1.5
Try it Yourself
2

Nullish operands & proportional split

The surprising rows mirror _.multiply: missing dividend returns the divisor; missing divisor returns the dividend; both missing returns 1. The ratio example splits 100 across weights 2:3.

javascript
import divide from "lodash/divide";

console.log(divide(undefined, 2));   // 2   (no division performed)
console.log(divide(10, undefined));  // 10
console.log(divide(undefined, undefined)); // 1

const total = 100;
const wA = 2;
const wB = 3;
const portionA = divide(total * wA, wA + wB); // 40
const portionB = divide(total * wB, wA + wB); // 60
console.log(portionA, portionB);
Try it Yourself
3

Divide by zero, strings, and 0 / 0

Lodash does not add guards: you get the same Infinity / NaN you would from /. String numerics still divide after coercion. For display rounding, import round alongside divide.

javascript
import divide from "lodash/divide";
import round from "lodash/round";

console.log(divide(10, 0));        // Infinity
console.log(divide(-10, 0));       // -Infinity
console.log(divide(0, 0));         // NaN

console.log(divide("10", 2));      // 5
console.log(divide(10, "2"));      // 5

console.log(round(divide(1, 3), 2)); // 0.33
Try it Yourself

📋 _.divide vs /

Expression_.divide(a, b)a / b
10, 255
10, 0InfinityInfinity
undefined, 22NaN
10, undefined10NaN
undefined, undefined1NaN
'10', '2'55
1n, 2nthrows0n (native BigInt division truncates toward zero)

The last BigInt row: native 1n / 2n is 0n; lodash throws before dividing because baseToNumber rejects BigInt.

Pitfalls to avoid

Myth

No divide-by-zero “protection”

JavaScript already returns Infinity for non-zero / 0. Lodash does not throw or return a sentinel—do not confuse convenience helpers with validation.

Semantics

Undefined is not “skip me”

_.divide(undefined, 2) === 2 is almost never what you mean in business logic. Guard inputs explicitly before calling divide.

Floats

Rounding display

Combine with _.round or fixed-point integers when showing currency; divide(20, 3) is still an endless binary expansion.

BigInt

Use native /

Passing BigInt throws from baseToNumber.

❓ FAQ

No—and neither does plain JavaScript: 10 / 0 is Infinity. Lodash simply performs the same IEEE-754 division after coercion. It does not catch or rewrite divide-by-zero into an error.
createMathOperation treats a missing first operand like multiply does: if dividend is undefined but divisor is defined, lodash returns the divisor without performing division. Symmetrically, _.divide(10, undefined) returns 10. Only both-undefined returns the default 1.
The factory default for multiply and divide is 1 (multiplicative identity). For add/subtract the default is 0.
Yes when one operand is a string both sides are passed through baseToString before the operator, then JavaScript's / coerces the string forms—'10'/'2' evaluates to 5. Prefer explicit Number() for clarity.
No. baseToNumber throws 'Cannot convert a BigInt value to a number'. Use native / on BigInts.

Summary

  • Purpose: pairwise division with createMathOperation nullish rules and coercion parity with _.multiply.
  • Remember: / semantics for zeros; undefined shortcuts return 1, the divisor, or the dividend—not NaN.
  • Next: Lodash _.floor() (when published), _.ceil(), or the official Lodash docs for _.divide.
Did you know?

_.divide is built with createMathOperation(function (a, b) { return a / b; }, 1)—the same factory as _.multiply, but the default when both arguments are undefined is 1 (multiplicative identity), not 0 like _.add. That is why _.divide(undefined, undefined) is 1, not NaN.

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.

6 people found this page helpful