Lodash _.toInteger() method

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

What you’ll learn

  • How _.toInteger builds on _.toFinite and removes the fractional part.
  • Why the rounding is truncation toward zero—the same as Math.trunc, not Math.floor.
  • How NaN, Number.MIN_VALUE, and ±Infinity are 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, and Math.trunc apart 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 NaN0; ±Infinity±Number.MAX_VALUE.

Inherits parsing

Trims whitespace, parses hex/binary/octal strings via _.toNumber.

Syntax

javascript
_.toInteger(value)
  • value: the value to convert.
  • Returns: a finite integer-valued number. Never NaN; never ±Infinity.
1

Lodash docs baseline

Four cases from the official docs—decimal, sub-1, infinity, numeric string.

javascript
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
);
Try it Yourself
2

Truncates toward zero (not floor!)

The single biggest gotcha: _.toInteger mirrors Math.trunc, not Math.floor. Watch the negative cases.

javascript
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
);
Try it Yourself
3

Strings, nullish, & numeric prefixes

Parsing tricks inherited from _.toNumber_.toFinite: whitespace trim, hex/binary/octal literals, and the universal “bad input → 0” fallback.

javascript
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
);
Try it Yourself

📋 _.toInteger vs other integer conversions

Input_.toIntegerMath.truncMath.floorparseInt
3.93333
-3.9-3-3-4-3
'abc'0NaNNaNNaN
Infinity1.79e+308InfinityInfinityNaN
'0x1f'31NaNNaN31 (radix 16)

Pitfalls to avoid

Rounding

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.

Tiny

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

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

No. _.toInteger truncates toward zero, like Math.trunc. _.toInteger(-42.7) returns -42; Math.floor(-42.7) returns -43. Don't confuse them—it's a common source of off-by-one bugs.
Number.MIN_VALUE is ~5e-324, well below 1. After _.toFinite preserves it, the result % 1 step removes the fractional part, leaving 0.
_.toInteger(Infinity) returns Number.MAX_VALUE (1.7976...e+308). It inherits _.toFinite's clamp—Infinity is already a finite multiple of 1 in IEEE-754, so no fractional remainder is removed.
Yes. Anything that fails to parse (e.g. 'abc') goes through _.toFinite, returning 0. Whitespace-trimmed numeric strings (' 42 ') and hex/binary/octal prefixes work.

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()_.toLength is the next step (array-length-safe integers).
Did you know?

_.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.

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