Lodash _.isSafeInteger() method
What you’ll learn
- How
_.isSafeInteger(value)mirrorsNumber.isSafeInteger. - Why floats,
Infinity, andNaNfail despite being numeric. - How the safe range stops at
±(2^53 − 1). - When to combine with parsing helpers before validating user input.
Prerequisites
You completed or skimmed _.isInteger — this helper adds a range check on top.
- You know
Number.MAX_SAFE_INTEGERis2^53 − 1. - Try-it labs load lodash from the CDN.
Overview
Reach for _.isSafeInteger when you need confidence that arithmetic, IDs, or counters won't silently lose precision once a value crosses the IEEE-754 safe-integer threshold.
Integer + range
Both checks must pass: integer-ness and within the safe band.
No coercion
Strings like "3" are rejected per the lodash docs.
Overflow-safe IDs
Guard counters and primary keys before storing them as numbers.
Syntax
_.isSafeInteger(value) - value: any value to test.
- Returns:
truewhen value is an integer with|value| ≤ Number.MAX_SAFE_INTEGER; otherwisefalse.
Small integers and the boundary
Whole numbers up to Number.MAX_SAFE_INTEGER pass; one past the edge fails.
import isSafeInteger from "lodash/isSafeInteger";
console.log(
"three: " + isSafeInteger(3) + "\n" + // true
"max: " + isSafeInteger(Number.MAX_SAFE_INTEGER) + "\n" + // true
"maxPlus1: " + isSafeInteger(Number.MAX_SAFE_INTEGER + 1) // false
); Floats, Infinity, and NaN
The lodash docs explicitly call out Number.MIN_VALUE and Infinity — both false.
import isSafeInteger from "lodash/isSafeInteger";
console.log(
"pi: " + isSafeInteger(3.14) + "\n" + // false
"minVal: " + isSafeInteger(Number.MIN_VALUE) + "\n" + // false (lodash docs)
"inf: " + isSafeInteger(Infinity) + "\n" + // false (lodash docs)
"nan: " + isSafeInteger(NaN) // false
); Strings, null, and BigInt
Lodash never coerces — "3" is a string, and BigInt values aren't Numbers.
import isSafeInteger from "lodash/isSafeInteger";
console.log(
"strThree: " + isSafeInteger("3") + "\n" + // false (lodash docs)
"nullVal: " + isSafeInteger(null) + "\n" + // false
"big: " + isSafeInteger(10n) // false (BigInt, not Number)
); 📋 _.isSafeInteger vs related checks
| API / pattern | Behavior |
|---|---|
_.isSafeInteger(x) | Integer within ±(2^53 − 1). |
Number.isSafeInteger(x) | Identical semantics — lodash wraps it. |
_.isInteger(x) | No range check — 2^53 passes here. |
_.isFinite(x) | Finite number, but may be a float or out of safe range. |
Pitfalls to avoid
64-bit identifiers
Backend IDs that exceed 2^53 − 1 should travel as strings; numbers will silently round.
Parse before validating
Convert raw text inputs via Number() or parseInt before calling this guard.
Need huge integers?
For arbitrarily large whole numbers, use BigInt and dedicated arithmetic instead of safe-integer checks.
❓ FAQ
Summary
- Purpose: confirm a value is an integer JavaScript can represent without precision loss.
- Remember: floats, infinities, strings, and BigInts always return
false. - Next: explore more on Lodash _.isSet().
JavaScript's safe-integer band stops at 2^53 − 1 (9,007,199,254,740,991)—past that, consecutive integers can collide because IEEE-754 doubles run out of mantissa bits.
6 people found this page helpful
