By the end of this tutorial, you’ll use Lodash’s _.stubArray() as a small factory that returns fresh empty arrays—ideal for defaults, fallbacks, and functional callbacks.
01
Core Syntax
_.stubArray()
02
Returns []
Real array.
03
Fresh each call
New reference.
04
Defaults
Param fallbacks.
05
As callback
Pass reference.
06
Stub family
With constant.
Fundamentals
What Is _.stubArray()?
_.stubArray is a function with no parameters that returns an empty array. Call it as _.stubArray() when you need [] right now, or pass _.stubArray (without parentheses) when another API wants a function that produces empty arrays—similar to writing () => [].
💡
Beginner tip — not an “array-like” object
_.stubArray() returns a normal JavaScript array ([]) with all standard array methods— not a plain object pretending to be an array.
It sits in Lodash’s stub helper family alongside _.stubFalse() and _.constant()—small building blocks for readable functional code.
Foundation
📝 Syntax
Invoke with no arguments:
javascript
_.stubArray()
Syntax Rules
_.stubArray — the function reference (pass to callbacks).
_.stubArray() — call it to get [] immediately.
Fresh arrays — each call returns a new empty array instance.
Zero args — ignores any arguments if callers pass them anyway.
Length zero — _.stubArray().length === 0.
javascript
import stubArray from "lodash/stubArray";
stubArray();
// []
Cheat Sheet
⚡ Quick Reference
Task
Code pattern
Notes
Get empty array
_.stubArray()
Immediate []
Default parameter
fn(x = _.stubArray())
When omitted
Fallback return
return data || _.stubArray()
Always array
Callback ref
_.times(3, _.stubArray)
Fn reference
Arrow equivalent
() => []
Same idea
Shared reference
_.constant([])
Same [] each call
Returns
[]
Empty array
Type
Function
Zero-arg
Each call
New []
Fresh ref
Category
Util
Stub
Reference
🧰 Parameters
_.stubArray takes no parameters—only returns a value when invoked:
argumentsNone
No configuration. Call _.stubArray() with an empty argument list.
_.stubArray()
returnArray
Empty array []—truthy in JavaScript but with length === 0.
[]
referencePer call
Successive calls return different array objects—safe to mutate independently.
a !== b
as callbackPattern
Pass _.stubArray without () when an iteratee should produce [].
_.times(n, _.stubArray)
Need a fixed value other than arrays? See _.constant() and the other stub helpers in this series.
Hands-On
Examples Gallery
Practical _.stubArray() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.
_.stubArray() is a tiny but expressive helper: a function that always produces fresh empty arrays. Use it for defaults, merge helpers, and fallback returns when you want Lodash-flavored clarity over raw [].
Remember the distinction from _.constant([])—stubArray is for independent empty arrays; constant is for a fixed shared value.
Use _.stubArray() for readable empty-array defaults in Lodash code
Return _.stubArray() when APIs must always yield an array type
Pass _.stubArray as a callback when iteratees should produce []
Prefer stubArray over constant([]) when results may be mutated
Combine with optional parameters for concat/merge helpers
❌ Don’t
Call it expecting a shared singleton array across calls
Confuse with array-like objects—it returns a true Array
Use || stubArray() when empty arrays are valid data—use ??
Reach for stubArray when a plain = [] default is clearer for your team
Mutate arrays returned from _.constant([]) thinking they are fresh
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.stubArray()
Use these points for empty-array defaults and fallbacks.
5
Core concepts
📦01
Returns []
Real array.
Core
🔄02
Fresh ref
Each call.
Behavior
✅03
Defaults
Params.
Usage
📝04
Callback
Pass ref.
Pattern
🛠05
vs constant
Shared vs new.
Compare
❓ Frequently Asked Questions
_.stubArray is a zero-argument function. Calling _.stubArray() returns a new empty JavaScript array []. It is a named helper for “always give me an empty array” in functional Lodash code.
[] is a value. _.stubArray is a function you call (or pass by reference) when an API expects a function that produces empty arrays—default parameters, _.times, _.cond handlers, or fallback returns.
Yes—each _.stubArray() invocation returns a new empty array. That makes it safe for defaults you might mutate, unlike reusing one shared [] constant.
Both belong to Lodash’s stub family. _.constant([]) returns the same array reference every call. _.stubArray() creates a new [] each time—better when callers might push or splice.
Yes: function fn(items = _.stubArray()) works. When the argument is omitted, Lodash’s stub runs and supplies a fresh empty array for that invocation.
Use it for empty-array fallbacks (return data || _.stubArray()), optional array parameters, and anywhere you want () => [] with a descriptive Lodash name.
Did you know?
Lodash documents _.times(2, _.stubArray) alongside other stub helpers—passing _.stubArray by reference lets _.times invoke it each iteration and collect separate empty arrays.