By the end of this tutorial, you’ll use Lodash’s _.stubTrue() as a named “always true” function—for predicates, mocks, and _.cond default branches.
01
Core Syntax
_.stubTrue()
02
Always true
Any args ignored.
03
Fn reference
_.stubTrue
04
Predicates
Filter / some.
05
vs constant
constant(true)
06
vs stubFalse
Opposite stub.
Fundamentals
What Is _.stubTrue()?
_.stubTrue is a zero-argument function that always returns true. It belongs to Lodash’s stub helper family alongside _.stubFalse() and _.stubString(). Under the hood it is equivalent to _.constant(true).
💡
Beginner tip — function vs boolean
_.stubTrue() gives you the value true. _.stubTrue (no parentheses) is the function itself—pass it when Lodash or your code expects a predicate callback.
Use it when you need a readable, reusable “always passes” check—clearer than sprinkling () => true through cond tables and filter pipelines.
Foundation
📝 Syntax
Invoke with no arguments (they are ignored anyway):
javascript
_.stubTrue()
Syntax Rules
_.stubTrue — function reference for predicates and callbacks.
_.stubTrue() — call once to get boolean true.
Ignores arguments — _.stubTrue(1, "x", {}) still returns true.
Cond default — in _.cond, pair _.stubTrue with a fallback handler.
Primitive true — returns the boolean true, not 1 or "yes".
javascript
import stubTrue from "lodash/stubTrue";
stubTrue();
// true
Cheat Sheet
⚡ Quick Reference
Task
Code pattern
Notes
Get true now
_.stubTrue()
Boolean value
Pass predicate
_.filter(arr, _.stubTrue)
Keep all items
Enabled check
const ok = _.stubTrue
Fn reference
Mock validator
validate: _.stubTrue
Always valid
Equivalent
_.constant(true)
Same behavior
Default in cond
_.stubTrue
Catch-all pair
Returns
true
Boolean
Type
Function
Zero-arg
Args
Ignored
Always true
Category
Util
Stub
Reference
🧰 Parameters
_.stubTrue accepts no meaningful parameters—callers may pass args, but stubTrue ignores them:
argumentsIgnored
Any values passed when stubTrue is used as a callback are discarded.
_.stubTrue(1, 2, 3)
returntrue
Always the boolean primitive true.
true
referencePattern
Pass _.stubTrue without () to APIs expecting a function.
_.some(arr, _.stubTrue)
call oncePattern
Use _.stubTrue() when you need the value—not a callable function reference.
return _.stubTrue()
Need always-false instead? See _.stubFalse()—the opposite stub for fail predicates and empty filters.
Hands-On
Examples Gallery
Practical _.stubTrue() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.
Default parameter uses the function reference so callers can swap in a real validator later.
🚀 Beyond the Basics
stubTrue vs constant, arrow, and stubFalse.
Example 6 — stubTrue vs alternatives
Pick the right always-true tool—and know when stubFalse is the opposite.
javascript
console.log(_.stubTrue());
console.log(_.constant(true)());
console.log((() => true)());
console.log(_.stubTrue === _.constant(true)); // often true in Lodash
// cond default — stubTrue is the catch-all pair:
// _.cond([[_.stubTrue, () => "default"]])
// true, true, true
📤 Console output:
true
true
true
When to use which
stubTrue for always-pass predicates and cond catch-alls; stubFalse for never-pass filters; plain true when a function is not required.
Compare
📋 _.stubTrue vs related patterns
Topic
_.stubTrue()
true
_.constant(true)
_.stubFalse()
Kind
Stub function
Boolean literal
Function factory
Stub function
Returns
true
true
true
false
As callback
_.stubTrue
Awkward
Yes
_.stubFalse
cond default
Yes
No
No
No
Best for
Catch-all true
Simple flags
Generic constant
Fail predicates
🧠 How _.stubTrue() Works
1
Invoke stub
Call directly or pass _.stubTrue as a callback.
Call
2
Skip logic
No conditionals—implementation always yields true.
Evaluate
3
Ignore args
Elements, indexes, or user data passed by iteratees are unused.
Ignore
=
🛠
true
Boolean primitive—passes every truthiness check and filter predicate.
Important
📝 Notes
_.stubTrue() returns true—do not call the result again as a function.
For callable enabled gates, assign const fn = _.stubTrue (reference).
Use stubTrue as the default branch in _.cond—not _.stubFalse.
Setting object fields with isAdmin: _.stubTrue() stores boolean true—same as true, not a function.
Equivalent to _.constant(true) for behavior; stubTrue is more readable in stub-family code.
Next in the series: _.times()—invoke iteratees n times.
Wrap Up
Conclusion
_.stubTrue() is a small named helper for “always true”—whether you need a boolean now or a predicate callback that always passes. Pair it with the rest of the stub family for consistent functional style.
Remember the parentheses rule: reference for callbacks, call when you want the value. For catch-all _.cond branches, stubTrue is the idiomatic choice.
Pass _.stubTrue to filter/some when everything should match
Use as a default validator that always passes in tests
Prefer stubTrue over () => true in Lodash cond/filter tables
Use _.stubTrue for cond default branches
Return true or _.stubTrue() directly when no callback is needed
❌ Don’t
Assign const f = _.stubTrue() then call f()
Use stubFalse as a cond catch-all—stubFalse is for fail predicates, not defaults
Expect stubTrue to return truthy non-booleans like 1 or "yes"
Put stubTrue in object literals expecting a function property without noticing ()
Confuse with _.noop—noop returns undefined, not true
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.stubTrue()
Use these points for always-true predicates and mocks.
5
Core concepts
📦01
Always true
Every call.
Core
🔄02
Fn vs ()
Ref vs value.
Pitfall
✅03
Filter all
Predicate.
Usage
📝04
constant
Same idea.
Related
🛠05
stubFalse
Opposite.
Compare
❓ Frequently Asked Questions
_.stubTrue is a zero-argument function that always returns true. Call _.stubTrue() when you need the boolean true immediately, or pass _.stubTrue (no parentheses) when an API expects a function that always passes its check.
true is a value. _.stubTrue is a function—useful when something expects a callback or predicate. _.stubTrue() evaluates to true, same as calling the function once.
_.stubTrue is sugar for _.constant(true). Both always return true and ignore arguments. stubTrue reads clearer in Lodash stub/cond style code.
Pass _.stubTrue to _.filter, _.some, or _.cond handlers that want a function. Use _.stubTrue() when you need the boolean value right now—like return true or assign a flag. Do not call it then try to invoke the result as a function.
stubTrue always returns true—common as the default branch in _.cond and for keep-all filter predicates. stubFalse always returns false—useful for disabled checks or empty filter results.
Use it for always-pass predicates, _.cond default branches, test mocks that always succeed, and anywhere () => true reads well but you prefer a named Lodash helper.
Did you know?
Lodash pairs _.stubTrue with _.cond for default branches because it always passes—use _.stubFalse when you need the opposite: predicates that never pass, like empty filters or failed validation mocks.