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)
Call
What it does
_.toSafeInteger(value)
Primary signature.
Prefer import toSafeInteger from "lodash/toSafeInteger" when you want a smaller bundle.
Library declarations
Lodash
_.toSafeInteger(value)
Parameters
Parameter
Description
value
Required. Value to convert
Return Value
Return Value
Description
*
Integer in ±MAX_SAFE_INTEGER
Lodash Examples
Example 1 — Official Basics
Decimals, tiny values, Infinity, and numeric strings.
_.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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.toSafeIntegerLodash 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.