Lodash _.toInteger() method
What you’ll learn
- How
_.toIntegerbuilds on_.toFiniteand removes the fractional part. - Why the rounding is truncation toward zero—the same as
Math.trunc, notMath.floor. - How
NaN,Number.MIN_VALUE, and±Infinityare handled. - That parsing tricks (hex, binary, octal, whitespace) carry through from
_.toNumber.
Prerequisites
You’ve finished the _.toFinite tutorial; that’s the engine running underneath.
- You can tell
Math.floor,Math.round, andMath.truncapart for negative inputs. - Try-it labs load lodash from the CDN.
Overview
The implementation is two lines: var r = _.toFinite(value); return r === r ? (r % 1 ? r − r % 1 : r) : 0; — subtract the fractional remainder, which always points toward zero.
Truncates toward 0
3.9 → 3, -3.9 → -3 (not -4).
No NaN, no Infinity
Falsy and NaN → 0; ±Infinity → ±Number.MAX_VALUE.
Inherits parsing
Trims whitespace, parses hex/binary/octal strings via _.toNumber.
Syntax
_.toInteger(value) - value: the value to convert.
- Returns: a finite integer-valued
number. NeverNaN; never±Infinity.
Lodash docs baseline
Four cases from the official docs—decimal, sub-1, infinity, numeric string.
import toInteger from "lodash/toInteger";
console.log(
"3.2: " + toInteger(3.2) + "\n" + // 3
"MIN_VALUE: " + toInteger(Number.MIN_VALUE) + "\n" + // 0
"Infinity: " + toInteger(Infinity) + "\n" + // 1.7976931348623157e+308
"'3.2' string: " + toInteger("3.2") // 3
); Truncates toward zero (not floor!)
The single biggest gotcha: _.toInteger mirrors Math.trunc, not Math.floor. Watch the negative cases.
import toInteger from "lodash/toInteger";
console.log(
"_.toInteger(-42.7): " + toInteger(-42.7) + "\n" + // -42 (toward zero)
"Math.trunc(-42.7): " + Math.trunc(-42.7) + "\n" + // -42 (matches)
"Math.floor(-42.7): " + Math.floor(-42.7) + "\n" + // -43 (differs!)
"_.toInteger(3.9): " + toInteger(3.9) + "\n" + // 3
"_.toInteger(-3.9): " + toInteger(-3.9) // -3
); Strings, nullish, & numeric prefixes
Parsing tricks inherited from _.toNumber → _.toFinite: whitespace trim, hex/binary/octal literals, and the universal “bad input → 0” fallback.
import toInteger from "lodash/toInteger";
console.log(
"'-3.2' string: " + toInteger("-3.2") + "\n" + // -3
"' 7 ' trim: " + toInteger(" 7 ") + "\n" + // 7
"'0x1f' hex: " + toInteger("0x1f") + "\n" + // 31
"'0b11' binary: " + toInteger("0b11") + "\n" + // 3
"'abc': " + toInteger("abc") + "\n" + // 0
"null: " + toInteger(null) // 0
); 📋 _.toInteger vs other integer conversions
| Input | _.toInteger | Math.trunc | Math.floor | parseInt |
|---|---|---|---|---|
3.9 | 3 | 3 | 3 | 3 |
-3.9 | -3 | -3 | -4 | -3 |
'abc' | 0 | NaN | NaN | NaN |
Infinity | 1.79e+308 | Infinity | Infinity | NaN |
'0x1f' | 31 | NaN | NaN | 31 (radix 16) |
Pitfalls to avoid
Not Math.floor
For negative decimals the two diverge: _.toInteger(-1.5) is -1; Math.floor(-1.5) is -2. If you need the floor, call Math.floor directly.
Sub-1 positives collapse to 0
Anything with magnitude < 1 (including 0.9999 and Number.MIN_VALUE) becomes 0. Don’t use _.toInteger to detect “is positive.”
BigInt throws
_.toInteger(1n) raises “Cannot convert a BigInt value to a number.” Convert via Number(bigint) first if you can accept precision loss.
❓ FAQ
Summary
- Purpose: hand back a finite integer for anything you throw at it (except BigInt).
- Remember: truncation toward zero, not floor.
_.toInteger(-1.5)is-1. - Next: head to Lodash _.toLength() —
_.toLengthis the next step (array-length-safe integers).
_.toInteger truncates toward zero, exactly like Math.trunc—_.toInteger(-42.7) is -42, not -43. Lodash’s source uses result − (result % 1), which is the same semantics ECMAScript’s spec calls ToInteger.
6 people found this page helpful
