By the end of this tutorial, you’ll use Lodash’s _.constant() to build functions that always return one fixed value—no matter what arguments callers pass.
01
Core Syntax
Wrap any value: const fn = _.constant(value).
02
Ignores Args
Callers can pass anything—return stays the same.
03
With _.cond
Default branch handlers and fixed outcomes.
04
Test Mocks
Stub callbacks that return preset data.
05
Stub Helpers
Relation to _.stubTrue and friends.
06
vs defaultTo
Fixed fn vs nullish fallback value.
Fundamentals
What Is _.constant()?
_.constant() is a Lodash util helper that turns any value into a zero-argument function (ignoring extras) returning that value every time. APIs that expect callbacks—_.map, event hooks, or _.cond handlers—often need a function even when the result never changes. _.constant(42) is cleaner than writing () => 42 repeatedly in Lodash-heavy code.
💡
Beginner tip
Think of _.constant("OK") as a vending machine that always dispenses the same item. Shoppers can press different buttons (arguments), but the output never changes.
It is a small building block behind Lodash stub utilities and default branches in functional conditionals—not a replacement for real business logic, but a handy adapter when a function signature is required.
Foundation
📝 Syntax
Pass the value you want every call to return:
javascript
_.constant(value)
Syntax Rules
value — any JavaScript value: number, string, boolean, object, array, or undefined.
Return value — a new function; each invocation returns the captured value.
Arguments ignored — fn(), fn(1, 2, 3) all return the same thing.
Reference types — objects and arrays are returned by reference, not cloned.
Not lazy — the value is fixed when you call _.constant(), not recomputed per call.
The mock object is created once. Every call to mockApiResponse() returns that same object reference—ideal for predictable test doubles.
Example 5 — Object reference caveat
Constants over objects share one reference—mutations affect later calls.
javascript
const payload = { count: 0 };
const getPayload = _.constant(payload);
const a = getPayload();
a.count = 5;
const b = getPayload();
console.log(b.count); // 5 — same object
// Safer: constant over a primitive
const alwaysZero = _.constant(0);
console.log(alwaysZero()); // 0
📤 Console output:
5
0
How It Works
_.constant does not clone. For immutable snapshots use Object.freeze, _.cloneDeep, or constant primitives instead.
🚀 Beyond the Basics
Compare stub helpers, arrows, and defaultTo.
Example 6 — constant vs stubTrue vs arrow vs defaultTo
Pick the right helper— they solve different problems.
javascript
// constant — any fixed return value
console.log(_.constant(42)()); // 42
// stubTrue — sugar for constant(true)
console.log(_.stubTrue()); // true
console.log(_.constant(true)()); // true
// arrow — native equivalent
console.log((() => 42)()); // 42
// defaultTo — NOT a function factory; nullish fallback
console.log(_.defaultTo(null, "guest")); // "guest"
console.log(_.defaultTo("Ada", "guest")); // "Ada"
📤 Console output:
42
true
true
42
guest
Ada
When to prefer constant
Use _.constant() when you need a function that always returns one value. Use _.defaultTo() when you have a nullable value and want a fallback—not a callback.
Compare
📋 _.constant vs related patterns
Topic
_.constant
_.stubTrue
() => value
_.defaultTo
Returns
Function
Function (true)
Function
Value (not fn)
Fixed output
Any value
Always true
Any value
Fallback if nullish
Ignores args
Yes
Yes
Yes
N/A (two args)
Typical use
Mocks, cond handlers
cond predicate default
General JS
Null coalescing data
Lodash category
Util
Util
Native
Util
🗸 How _.constant() Works
1
Capture value
Lodash closes over the value you pass to _.constant().
Setup
2
Return wrapper fn
You get a function with no conditional logic—only the closure.
Factory
3
Invoke anytime
Callers may pass zero or many arguments—all ignored.
Call
=
📎
Same value every time
The closed-over value is returned on every invocation.
Important
📝 Notes
_.constant() creates a function, not the value itself—call it with () when you need the payload.
Object and array constants return the same reference; clone if callers mutate results.
Do not use as a default parameter value for strings—use name = "Guest" directly instead.
_.stubTrue, _.stubFalse, _.stubArray, and _.stubObject are specialized constants.
For nullable data fallback use _.defaultTo(), not constant.
Pairs naturally with _.cond() fixed-outcome handlers.
Wrap Up
Conclusion
_.constant() is a tiny but useful factory: turn any value into a function that always returns it. Reach for it in cond tables, test mocks, and anywhere a callback slot needs a predictable, argument-proof result.
Remember reference semantics for objects, and do not confuse it with _.defaultTo()—that helper fixes nullish data, not function signatures.
Use for cond handlers that return fixed strings or numbers
Use for test doubles and demo callbacks with stable payloads
Prefer primitives or frozen objects when immutability matters
Reach for _.stubTrue when you specifically need constant true
Document why a no-op callback returns a fixed value
❌ Don’t
Wrap _.constant inside default parameters for plain strings
Assume object constants are deep-cloned snapshots
Replace _.defaultTo with constant—they solve different jobs
Use constant when real logic should inspect arguments
Overuse where a literal default argument is clearer
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.constant()
Use these points when you need fixed-return functions.
5
Core concepts
📎01
Fixed fn
Always same return.
Basics
🚫02
Ignores args
Any call signature.
Behavior
📋03
cond
Fixed handlers.
Pattern
🧪04
Mocks
Test callbacks.
Practical
→05
defaultTo
Different helper.
Next step
❓ Frequently Asked Questions
_.constant(value) returns a new function. Every time you call that function—with zero arguments or many—it returns the same value that was passed to _.constant(). Arguments are completely ignored.
_.constant() always returns its fixed value. _.defaultTo(value, default) picks value unless value is null or undefined. Use constant for function factories; use defaultTo for fallback values on nullable data.
No. If you pass an object or array, the returned function always returns the same reference. Mutating that object elsewhere changes what future calls return. Pass primitives or freeze objects when you need immutability.
Lodash stub helpers like _.stubTrue, _.stubFalse, and _.stubArray are implemented with _.constant() internally. stubTrue is equivalent to _.constant(true).
Use it for default handlers in _.cond, no-op callbacks, test mocks that return fixed payloads, and anywhere an API expects a function but you only need a fixed return value.
Yes for simple cases. _.constant() is slightly more explicit in functional Lodash code and matches stub/cond patterns. Arrow functions are fine when you do not need a named lodash helper.
Did you know?
Lodash’s stub utilities are thin wrappers around _.constant(): stubTrue is constant(true), stubArray is constant([]), and stubObject is constant({}). Learning constant unlocks the whole stub family.