Lodash _.stubArray() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

Returns a new empty array every time it is called. Part of Lodash Util since 4.13.0 (stable — not deprecated).

Note: Each call allocates a fresh [] — two results are never the same reference (=== is false).

Syntax

Call _.stubArray. Start with this line.

Lodash
_.stubArray()
CallWhat it does
_.stubArray()Primary signature.
_.stubArray(...)Call the util helper.

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

Library declarations
Lodash
_.stubArray()

Parameters

This method has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*A new empty array

Lodash Examples

Example 1 — Official: With _.times

Build several empty arrays by repeating the stub.

Lodash
var arrays = _.times(2, _.stubArray);

console.log(arrays);
// → [[], []]
Try It Yourself

How It Works

  1. times. _.times(2, …) calls the iteratee twice.
  2. stubArray. Each call returns a new [].
  3. Collect. Results are gathered into an outer array.
  4. Result. [[], []] — two empty arrays.

Example 2 — Official: Distinct References

Each empty array is a different object in memory.

Lodash
var arrays = _.times(2, _.stubArray);

console.log(arrays[0] === arrays[1]);
// → false
Try It Yourself

How It Works

  1. Two calls. Two separate allocations.
  2. ===. Reference equality checks the same object.
  3. False. Different arrays → not identical.
  4. Why it matters. Mutating one does not change the other.

Example 3 — Call Directly

You can invoke the stub without _.times.

Lodash
_.stubArray();
// → []

_.stubArray().push(1);
// → [1]  // returned array is yours to mutate
Try It Yourself

How It Works

  1. Call. _.stubArray() returns [].
  2. Own it. The array is a normal value you can mutate.
  3. Next call. A later _.stubArray() is a different empty array.
  4. Use. Good default return for “give me a list to fill.”

Example 4 — stubArray vs constant([])

One creates new arrays; the other reuses one shared array.

Lodash
var a = _.times(2, _.stubArray);
var b = _.times(2, _.constant([]));

a[0] === a[1];
// → false

b[0] === b[1];
// → true  // same reference!
Try It Yourself

How It Works

  1. stubArray. Fresh [] per call → === is false.
  2. constant. _.constant([]) always returns the same array.
  3. Risk. Mutating b[0] also changes b[1].
  4. Rule. Need isolated empties → stubArray.

Example 5 — Safe Mutation per Bucket

Create separate buckets, then push into each independently.

Lodash
var buckets = _.times(2, _.stubArray);
buckets[0].push('a');
buckets[1].push('b');

console.log(buckets);
// → [['a'], ['b']]
Try It Yourself

How It Works

  1. Allocate. Two empty arrays via stubArray.
  2. Push. Each bucket gets its own value.
  3. Isolated. No shared reference bleed.
  4. Pattern. Common when initializing groups or columns.

Browser & Runtime Support

_.stubArray is a Lodash Util helper since 4.13.0. It runs anywhere Lodash 4.13+ / 4.x runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.13.0+

_.stubArray()

Available in [email protected] full builds and the lodash/stubArray modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.13+
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
_.stubArray Lodash 4.13.0+

Bottom line: Safe for production once Lodash 4.13+ is loaded. Prefer import stubArray from "lodash/stubArray" for smaller bundles. Each call returns a new empty array—safer than _.constant([]) when results may be mutated.

Frequently Asked Questions

_.stubArray() takes no arguments and returns a new empty array ([]). Each call creates a fresh array instance.
They behave the same. _.stubArray is a named Lodash helper—handy as a default callback, with _.times, or when an API expects a function reference.
_.constant([]) always returns the same array reference. _.stubArray() returns a new [] every time—safer when callers might mutate the result.
No. Official docs: arrays[0] === arrays[1] is false because each call allocates a new array.
Use it as a default empty-array factory, a no-op list producer in maps/times, or a placeholder callback that must return an array type.
Yes. Any arguments are ignored—the return is always a new empty array.

Next: _.stubFalse()

A stub that always returns false — useful as a default predicate.

_.stubFalse() 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