Lodash _.toLength() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

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)
CallWhat it does
_.toLength(value)Primary signature.

Prefer import toLength from "lodash/toLength" when you want a smaller bundle.

Library declarations
Lodash
_.toLength(value)

Parameters

ParameterDescription
valueRequired. Any value to convert

Return Value

Return ValueDescription
*Integer in [0, 2^32−1]

Lodash Examples

Example 1 — Docs: Number and Numeric String

Decimals become whole lengths; strings parse first.

Lodash
_.toLength(3.2);
// → 3

_.toLength("3.2");
// → 3
Try It Yourself

How It Works

  1. toInteger. 3.2 truncates toward zero → 3.
  2. Clamp. 3 is already inside [0, max].
  3. String. "3.2" parses the same way.
  4. Same as docs. Matches the common Lodash examples.

Example 2 — Docs: Number.MIN_VALUE → 0

Tiny positives disappear after integer conversion.

Lodash
_.toLength(Number.MIN_VALUE);
// → 0

_.toInteger(Number.MIN_VALUE);
// → 0
Try It Yourself

How It Works

  1. Tiny. MIN_VALUE is about 5e-324.
  2. Truncate. Absolute value < 1 → integer 0.
  3. Clamp. 0 is already the lower bound.
  4. Docs. Same result as the published example.

Example 3 — Docs: Infinity → 4294967295

Huge values stop at the max legal array length.

Lodash
_.toLength(Infinity);
// → 4294967295

_.toInteger(Infinity);
// → 1.7976931348623157e+308
Try It Yourself

How It Works

  1. toInteger. Maps Infinity to Number.MAX_VALUE.
  2. Clamp. Caps at 2^32−1.
  3. Contrast. toInteger alone keeps the huge finite number.
  4. Why. Arrays cannot be longer than that max.

Example 4 — Negatives and Oversized Values

The clamp is what makes toLength different from toInteger.

Lodash
_.toLength(-5);
// → 0

_.toInteger(-5);
// → -5

_.toLength(4294967296);
// → 4294967295

_.toLength(-Infinity);
// → 0
Try It Yourself

How It Works

  1. Floor. Negatives clamp up to 0.
  2. Ceiling. Anything above max becomes 4294967295.
  3. toInteger. Still returns -5 — no length clamp.
  4. Silent. No throw — validate if zero must mean “empty,” not “bad.”

Example 5 — Safe new Array(n)

Sanitize untrusted sizes before allocating.

Lodash
function makeBuffer(requested) {
  var size = _.toLength(requested);
  return new Array(size);
}

makeBuffer(3.9).length;
// → 3

makeBuffer(-1).length;
// → 0

makeBuffer(9e99).length;
// → 4294967295
Try It Yourself

How It Works

  1. Sanitize. toLength guarantees a legal length.
  2. Allocate. new Array(size) will not throw “Invalid array length.”
  3. Truncate. 3.9 becomes length 3.
  4. Cap. Extreme inputs hit the max — still valid, still large.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.toLength Lodash 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.

Next: _.toNumber()

Convert a value to a number — the raw coercion behind these helpers.

_.toNumber() tutorial →

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.

10 people found this page helpful