Lodash _.divide() method
What you’ll learn
- How
_.divide(dividend, divisor)wraps native/with_.toNumber/ string handling fromcreateMathOperation. - The multiplicative identity rules: both
undefined→1; one side missing → return the other operand unchanged. - That divide-by-zero yields
±InfinityorNaNexactly like JavaScript—lodash does not throw or “sanitize” it. - A practical split: proportional shares with
_.divideplus_.roundwhen 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 / 0isInfinity;0 / 0isNaN. createMathOperation: same helper as_.addand_.multiply—compare defaults0vs1.
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
_.divide(dividend, divisor) - dividend: numerator (first operand).
- divisor: denominator (second operand).
- Returns: quotient
dividend / divisorafter coercion, or one of the nullish shortcuts documented below.
Lodash docs baseline
The single documented example: six divided by four.
import divide from "lodash/divide";
const q = divide(6, 4);
console.log(q);
// => 1.5 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.
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); 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.
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 📋 _.divide vs /
| Expression | _.divide(a, b) | a / b |
|---|---|---|
10, 2 | 5 | 5 |
10, 0 | Infinity | Infinity |
undefined, 2 | 2 | NaN |
10, undefined | 10 | NaN |
undefined, undefined | 1 | NaN |
'10', '2' | 5 | 5 |
1n, 2n | throws | 0n (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
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.
Undefined is not “skip me”
_.divide(undefined, 2) === 2 is almost never what you mean in business logic. Guard inputs explicitly before calling divide.
Rounding display
Combine with _.round or fixed-point integers when showing currency; divide(20, 3) is still an endless binary expansion.
Use native /
Passing BigInt throws from baseToNumber.
❓ FAQ
Summary
- Purpose: pairwise division with
createMathOperationnullish rules and coercion parity with_.multiply. - Remember:
/semantics for zeros;undefinedshortcuts return1, the divisor, or the dividend—notNaN. - Next: Lodash _.floor() (when published), _.ceil(), or the official Lodash docs for _.divide.
_.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.
6 people found this page helpful
