Lodash _.isSafeInteger() method

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

What you’ll learn

  • How _.isSafeInteger(value) mirrors Number.isSafeInteger.
  • Why floats, Infinity, and NaN fail 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_INTEGER is 2^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

javascript
_.isSafeInteger(value)
  • value: any value to test.
  • Returns: true when value is an integer with |value| ≤ Number.MAX_SAFE_INTEGER; otherwise false.
1

Small integers and the boundary

Whole numbers up to Number.MAX_SAFE_INTEGER pass; one past the edge fails.

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

Floats, Infinity, and NaN

The lodash docs explicitly call out Number.MIN_VALUE and Infinity — both false.

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

Strings, null, and BigInt

Lodash never coerces — "3" is a string, and BigInt values aren't Numbers.

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

📋 _.isSafeInteger vs related checks

API / patternBehavior
_.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

IDs

64-bit identifiers

Backend IDs that exceed 2^53 − 1 should travel as strings; numbers will silently round.

Forms

Parse before validating

Convert raw text inputs via Number() or parseInt before calling this guard.

BigInt

Need huge integers?

For arbitrarily large whole numbers, use BigInt and dedicated arithmetic instead of safe-integer checks.

❓ FAQ

An integer that round-trips exactly through an IEEE-754 double—values from -(2^53-1) to 2^53-1 (Number.MIN_SAFE_INTEGER..Number.MAX_SAFE_INTEGER).
Lodash never coerces strings. Convert with Number(value) first if your input arrives as text.
No. Infinity isn't an integer at all, so lodash returns false (per the docs).
_.isInteger only checks integer-ness; isSafeInteger additionally enforces the safe range, so 2^53 is integer but not safe.

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().
Did you know?

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.

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