Lodash _.toInteger() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

Converts value to an integer. Runs through _.toFinite, then drops the fractional part toward zero (like Math.trunc). Part of Lodash 4.x Lang (since 4.0.0).

Pass the value you want to inspect or convert. Read the return type carefully before you use the result.

Syntax

Call _.toInteger. Start with this line.

Lodash
_.toInteger(value)
CallWhat it does
_.toInteger(value)Primary signature.

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

Library declarations
Lodash
_.toInteger(value)

Parameters

ParameterDescription
valueRequired. Any value to convert

Return Value

Return ValueDescription
*Integer number (no fraction)

Lodash Examples

Example 1 — Official: Number and Numeric String

Decimals become whole numbers; strings parse first.

Lodash
_.toInteger(3.2);
// → 3

_.toInteger("3.2");
// → 3
Try It Yourself

How It Works

  1. Finite. 3.2 stays 3.2 after toFinite.
  2. Drop. Fractional 0.2 is removed → 3.
  3. String. "3.2" parses to 3.2, then truncates the same way.
  4. vs toFinite. _.toFinite(3.2) would keep 3.2.

Example 2 — Official: Number.MIN_VALUE → 0

Tiny positives disappear once the fraction is stripped.

Lodash
_.toInteger(Number.MIN_VALUE);
// → 0

_.toFinite(Number.MIN_VALUE);
// → 5e-324
Try It Yourself

How It Works

  1. toFinite. Keeps the tiny positive value.
  2. Truncate. Anything with absolute value < 1 becomes 0.
  3. Contrast. toFinite alone still returns 5e-324.
  4. Official. Matches the Lodash docs example.

Example 3 — Official: Infinity → MAX_VALUE

Inherits the toFinite clamp, then there is no fraction left to drop.

Lodash
_.toInteger(Infinity);
// → 1.7976931348623157e+308

_.toInteger(Infinity) === Number.MAX_VALUE;
// → true
Try It Yourself

How It Works

  1. Clamp. toFinite(Infinity) → Number.MAX_VALUE.
  2. Integer step. No fractional part to remove.
  3. Result. Same large finite number.
  4. Safe integer? For ±(2^53−1) bounds use toSafeInteger.

Example 4 — Toward Zero (Not Math.floor)

Negatives are the easy place to mix this up.

Lodash
_.toInteger(-42.7);
// → -42

Math.trunc(-42.7);
// → -42

Math.floor(-42.7);
// → -43
Try It Yourself

How It Works

  1. Toward zero. Drop the fraction; keep the sign of the integer part.
  2. Match trunc. Same result as Math.trunc.
  3. Not floor. Math.floor goes more negative.
  4. Bug watch. Using floor for “integerize” on negatives is a common off-by-one.

Example 5 — Invalid Values Become 0

Same fallback idea as toFinite after a failed parse.

Lodash
_.toInteger(NaN);
// → 0

_.toInteger(null);
// → 0

_.toInteger("abc");
// → 0

_.toInteger("  42  ");
// → 42
Try It Yourself

How It Works

  1. Parse fail. Junk becomes NaN → toFinite maps to 0.
  2. null. Ends up as 0.
  3. Trim. Whitespace around digits is fine.
  4. Caution. Zero may hide bad input — validate when 0 is meaningful.

Browser & Runtime Support

_.toInteger is a Lodash 4.x Lang helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Closest native cousin is Math.trunc(Number(value)), but Lodash also applies toFinite clamping and soft-fails bad strings to 0.

✓ Stable · Lodash 4.x

_.toInteger()

Available in [email protected] full builds and the lodash/toInteger 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
_.toInteger Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import toInteger from "lodash/toInteger" for smaller builds. Ideal for indexes, counters, and whole-number form sanitizing.

Frequently Asked Questions

_.toInteger(value) converts value to a finite number, then removes the fractional part toward zero — like Math.trunc, not Math.floor.
No. For negatives they differ: _.toInteger(-42.7) is -42; Math.floor(-42.7) is -43.
toInteger runs toFinite first, then drops the fraction. So Infinity still becomes Number.MAX_VALUE.
MIN_VALUE is about 5e-324. After conversion, removing the fractional part leaves 0.
toSafeInteger also clamps into the safe integer range (±(2^53−1)). toInteger does not — Infinity becomes MAX_VALUE.
Use import toInteger from "lodash/toInteger" or require("lodash/toInteger") for tree-shaking. Full build: import _ from "lodash" then _.toInteger.

Next: _.toLength()

Convert a value to an integer suitable as an array-like length.

_.toLength() 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