Lodash _.isLength() method
What you’ll learn
- How
_.isLength(value)validates ECMAScript-style lengths for array-like data. - Why zero is allowed while negatives and non-integers are rejected.
- How the
MAX_SAFE_INTEGERceiling mirrors safe indexing assumptions. - When to pair lodash checks with explicit coercion from user input.
Prerequisites
You know JavaScript arrays and the idea of an array-like length property.
- You are comfortable with
typeoffor primitives versus strings. - Try-it labs load lodash from the CDN.
Overview
Reach for _.isLength when mirroring internal lodash guards—before slicing synthetic buffers, validating pagination totals destined for array allocation, or asserting object descriptors shaped like indexed collections.
Typed primitive
Only real number primitives—not numeric strings—pass the first gate.
Whole & non-negative
Integral constraint plus > -1 matches ToLength intuition.
Safe ceiling
Lengths cannot exceed MAX_SAFE_INTEGER.
Syntax
_.isLength(value) - value: any value to test.
- Returns:
truewhenvalueis a number suitable as an array-like length; otherwisefalse.
Zero and typical counts
Empty collections use length 0; positive integers behave as expected.
import isLength from "lodash/isLength";
console.log(
"zero: " + isLength(0) + "\n" + // true
"ten: " + isLength(10) // true
); Negatives, fractions, and Infinity
Lengths cannot be negative, fractional, or non-finite—matching lodash docs.
import isLength from "lodash/isLength";
console.log(
"negOne: " + isLength(-1) + "\n" + // false
"float: " + isLength(3.14) + "\n" + // false
"inf: " + isLength(Infinity) + "\n" + // false
"minVal: " + isLength(Number.MIN_VALUE) // false
); MAX_SAFE_INTEGER boundary
The inclusive upper bound is 9007199254740991; one step past fails.
import isLength from "lodash/isLength";
var MAX_SAFE_INTEGER = 9007199254740991;
console.log(
"maxSafe: " + isLength(MAX_SAFE_INTEGER) + "\n" + // true
"pastSafe: " + isLength(MAX_SAFE_INTEGER + 1) // false
); 📋 _.isLength vs related checks
| API | Behavior |
|---|---|
_.isLength(x) | Non-negative integral number ≤ MAX_SAFE_INTEGER. |
_.isInteger(x) | Any signed integral primitive number; no upper bound like lengths. |
Number.isSafeInteger(x) | Similar safe range but allows negatives when mathematically integer. |
Array.isArray(x) | Detects real arrays—orthogonal to validating a standalone length value. |
Pitfalls to avoid
String lengths from DOM
Attributes and query params arrive as strings—parse and validate NaN before lodash.
Rounding artifacts
Division can yield 3.0000000000000004; trim or round if you intend whole lengths.
Logical vs physical size
Extremely large conceptual counts may exceed the safe bound—design APIs accordingly.
❓ FAQ
Summary
- Purpose: certify numeric values that could legally back an array-like
length. - Remember: negatives are never lengths—even if they are integers elsewhere.
- Next: explore more on Lodash _.isMap().
_.isLength requires typeof value === "number", value > -1, value % 1 === 0, and value <= 9007199254740991 (MAX_SAFE_INTEGER)—so negative integers like -5 pass _.isInteger but never _.isLength.
6 people found this page helpful
