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