Lodash _.toSafeInteger() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

Converts value to a safe integer — an integer that can be compared and represented exactly in JavaScript numbers. Built on truncate-then-clamp to ±(253 − 1). Part of Lodash 4.x Lang.

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

Syntax

Call _.toSafeInteger. Start with this line.

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

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

Library declarations
Lodash
_.toSafeInteger(value)

Parameters

ParameterDescription
valueRequired. Value to convert

Return Value

Return ValueDescription
*Integer in ±MAX_SAFE_INTEGER

Lodash Examples

Example 1 — Official Basics

Decimals, tiny values, Infinity, and numeric strings.

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

_.toSafeInteger(Number.MIN_VALUE);
// → 0

_.toSafeInteger(Infinity);
// → 9007199254740991

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

How It Works

  1. 3.2. Truncates toward zero → 3.
  2. MIN_VALUE. Too small to keep as a non-zero integer → 0.
  3. Infinity. Clamped to MAX_SAFE_INTEGER.
  4. String. Parsed then truncated like the number case.

Example 2 — vs _.toInteger

Huge magnitudes stay huge with toInteger; safe clamps them.

Lodash
_.toInteger(1e20);
// → 100000000000000000000   // not safely exact

_.toSafeInteger(1e20);
// → 9007199254740991

_.toInteger(3.9);
// → 3

_.toSafeInteger(3.9);
// → 3
Try It Yourself

How It Works

  1. Same truncate. Both drop the fractional part toward zero.
  2. Huge value. toInteger keeps the unsafe magnitude.
  3. Clamp. toSafeInteger caps at MAX_SAFE_INTEGER.
  4. Small decimals. Results match when already in range.

Example 3 — ±Infinity Clamp

Infinities become the safe extremes.

Lodash
_.toSafeInteger(Infinity);
// → 9007199254740991

_.toSafeInteger(-Infinity);
// → -9007199254740991

Number.MAX_SAFE_INTEGER === 9007199254740991;
// → true
Try It Yourself

How It Works

  1. +∞. Clamped to the maximum safe integer.
  2. −∞. Clamped to the minimum safe integer.
  3. Never Infinity. Return type is always a finite integer.
  4. Constant. Matches Number.MAX_SAFE_INTEGER.

Example 4 — NaN and null

Invalid / empty-ish coercions settle at 0.

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

_.toSafeInteger(null);
// → 0

_.toSafeInteger(undefined);
// → 0

_.toSafeInteger("abc");
// → 0
Try It Yourself

How It Works

  1. NaN path. Integer conversion treats NaN as 0.
  2. null. Coerces like +null → 0.
  3. undefined / junk. Become NaN then 0.
  4. Stable. You always get a finite integer back.

Example 5 — vs _.toLength

Different clamp windows for different jobs.

Lodash
_.toSafeInteger(-5);
// → -5

_.toLength(-5);
// → 0                    // lengths cannot be negative

_.toSafeInteger(1e20);
// → 9007199254740991

_.toLength(1e20);
// → 4294967295           // 2^32 − 1
Try It Yourself

How It Works

  1. Negatives. Safe integer keeps them; length forces 0.
  2. Huge positive. Both clamp — different ceilings.
  3. toLength. Cap is 232 − 1 (array length).
  4. Choose. IDs/counts → safe; array sizes → length.

Browser & Runtime Support

_.toSafeInteger is a Lodash 4.x Lang helper. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native cousins: Number.isSafeInteger (check only) and manual clamp after Math.trunc.

✓ Stable · Lodash 4.x

_.toSafeInteger()

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

Bottom line: Safe for production once Lodash is loaded. Prefer import toSafeInteger from "lodash/toSafeInteger" for smaller builds. Use for IDs, counts, and values that must stay exactly representable.

Frequently Asked Questions

It converts value to an integer, then clamps it to ±Number.MAX_SAFE_INTEGER (±(2⁵³ − 1)) so the result can be represented exactly in IEEE-754 doubles.
Same truncate-toward-zero step, but toSafeInteger also clamps huge magnitudes. _.toInteger(1e20) stays huge; _.toSafeInteger(1e20) becomes 9007199254740991.
toLength clamps to [0, 2³² − 1] for array lengths. toSafeInteger allows negatives and a much larger upper bound.
Infinity → MAX_SAFE_INTEGER; −Infinity → −MAX_SAFE_INTEGER; NaN → 0.
No. It produces a safe integer. Use _.isSafeInteger (or Number.isSafeInteger) to test an existing number.
No. Conversion goes through number coercion and throws Cannot convert a BigInt value to a number.

Next: _.toString()

Convert values to strings with Lodash’s nullish and −0 rules.

_.toString() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful