Converts value to an integer suitable as the length of an array-like object. Runs _.toInteger, then clamps to [0, 232−1] (4294967295). Part of Lodash 4.x Lang (since 4.0.0).
Pass the value you want to inspect or convert. Read the return type carefully before you use the result.
Syntax
Call _.toLength. Start with this line.
Lodash
_.toLength(value)
Call
What it does
_.toLength(value)
Primary signature.
Prefer import toLength from "lodash/toLength" when you want a smaller bundle.
Library declarations
Lodash
_.toLength(value)
Parameters
Parameter
Description
value
Required. Any value to convert
Return Value
Return Value
Description
*
Integer in [0, 2^32−1]
Lodash Examples
Example 1 — Docs: Number and Numeric String
Decimals become whole lengths; strings parse first.
Allocate.new Array(size) will not throw “Invalid array length.”
Truncate.3.9 becomes length 3.
Cap. Extreme inputs hit the max — still valid, still large.
Compatibility
Browser & Runtime Support
_.toLength is a Lodash 4.x Lang helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. It mirrors ECMAScript ToLength; there is no single built-in of the same name, so Lodash (or a manual clamp) is the practical API.
✓ Stable · Lodash 4.x
_.toLength()
Available in [email protected] full builds and the lodash/toLength 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
_.toLengthLodash 4.x+
Bottom line: Safe for production once Lodash is loaded. Prefer import toLength from "lodash/toLength" for smaller builds. Ideal before new Array(n), indexed loops, and untrusted size inputs.
Frequently Asked Questions
_.toLength(value) converts value to an integer via toInteger, then clamps it into [0, 2^32−1] so it is safe as an array-like length.
4294967295 (2^32 − 1) — the largest legal Array length in JavaScript.
toInteger can return negatives and huge values. toLength forces negatives to 0 and caps at 4294967295.
toInteger(Infinity) is Number.MAX_VALUE; toLength then clamps that down to the max array length.
Yes. The result is always in the legal length range, so you avoid the “Invalid array length” error.
Use import toLength from "lodash/toLength" or require("lodash/toLength") for tree-shaking. Full build: import _ from "lodash" then _.toLength.