By the end of this tutorial, you’ll use Lodash’s _.defaultTo() to supply fallbacks when values are null or undefined—without accidentally wiping out 0 or empty strings.
01
Core Syntax
_.defaultTo(value, fallback) in one call.
02
Nullish Only
Replaces null and undefined—not all falsy values.
03
Keep 0 & ""
Zero and empty string stay intact.
04
Config Defaults
Fill missing API and app settings safely.
05
vs || and ??
Pick the right fallback operator.
06
vs defaults
Single value vs object merge helper.
Fundamentals
What Is _.defaultTo()?
_.defaultTo() is a Lodash util helper that returns a fallback value only when the first argument is null or undefined. Otherwise it returns the original value unchanged. It is the Lodash expression of nullish coalescing—cleaner than writing value != null ? value : fallback repeatedly in config loaders and data mappers.
💡
Beginner tip — empty string is not missing
_.defaultTo("", "guest") returns "", not "guest". An empty string is a deliberate value (user cleared the field). Only use || if you truly want to replace empty strings too.
Reach for _.defaultTo() when optional properties from JSON, query strings, or database rows may be absent, but legitimate falsy values like 0 or false should survive.
Foundation
📝 Syntax
Pass the value to test and the fallback to use when it is nullish:
javascript
_.defaultTo(value, defaultValue)
Syntax Rules
value — the value you have (may be null or undefined).
defaultValue — returned only when value is null or undefined.
Falsy but kept — 0, "", false, and NaN are not replaced.
Return value — either value or defaultValue (not a function).
Native twin — value ?? defaultValue behaves the same way.
javascript
import defaultTo from "lodash/defaultTo";
const name = defaultTo(undefined, "Guest");
// -> "Guest"
const count = defaultTo(0, 10);
// -> 0 (zero is kept)
Cheat Sheet
⚡ Quick Reference
Input value
_.defaultTo(v, "fb")
v || "fb"
undefined
"fb"
"fb"
null
"fb"
"fb"
""
""
"fb"
0
0
"fb"
false
false
"fb"
"Ada"
"Ada"
"Ada"
Triggers
null | undefined
Nullish only
Native
??
Same behavior
Object merge
_.defaults
Different helper
Category
Util
Value fallback
Reference
🧰 Parameters
Arguments to _.defaultTo() and what each controls:
valueRequired
The value to return when it is defined (including empty string, zero, and false). Lodash checks only for null and undefined.
_.defaultTo(user.name, "Guest")
defaultValueRequired
The fallback when value is nullish. Can be any type—string, number, object, or even null if you intentionally want null as the default.
_.defaultTo(cfg.timeout, 5000)
return valuevalue | default
Not a function—immediately resolves to one of the two inputs. Safe to embed in object literals and return statements.
return _.defaultTo(n, 0)
not for ""Important
To treat empty string as missing, combine checks: value === "" ? fallback : _.defaultTo(value, fallback) or use || deliberately.
_.defaultTo("", "x") // ""
For filling multiple missing object properties at once, see _.defaults() in the object category—not _.defaultTo().
Hands-On
Examples Gallery
Practical _.defaultTo() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.
📚 Getting Started
Replace null and undefined while keeping other values intact.
Example 1 — Fallback for null and undefined
When a property is missing from parsed JSON, supply a display name.
Use _.defaultTo() for single-value nullish fallback in Lodash pipelines. Use ?? in modern JS without Lodash. Use _.defaults() when merging whole option objects.
Compare
📋 _.defaultTo vs related patterns
Topic
_.defaultTo
??
||
_.defaults
Scope
Single value
Single value
Single value
Object merge
Replaces
null, undefined
null, undefined
All falsy
undefined keys on dest
Keeps 0 and ""
Yes
Yes
No
Yes (if set)
Returns
Value
Value
Value
Mutated object
Best for
Lodash pipelines
Modern JS
Any falsy fallback
Options objects
🧠 How _.defaultTo() Works
1
Receive value + default
Lodash takes the candidate value and the fallback to use if missing.
Input
2
Nullish check
Tests whether value is null or undefined—and nothing else.
Test
3
Pick result
Return defaultValue when nullish; otherwise return value unchanged.
Return
=
📊
Safe fallback
Falsy-but-valid values like 0 and "" survive the check.
Important
📝 Notes
Only null and undefined trigger the fallback—not empty strings or zero.
Equivalent to native value ?? defaultValue for the same inputs.
Do not nest multiple _.defaultTo() calls—one per value is clearer.
Centralize shared defaults in a constants module for consistency across the app.
If you need to replace empty strings, add an explicit check or use || knowingly.
Wrap Up
Conclusion
_.defaultTo() is a small, precise helper: when data might be missing (null or undefined), supply a fallback without clobbering legitimate 0, false, or "" values.
Pair it with clear default constants, compare consciously with ||, and reach for _.defaults() when you are merging entire options objects.
Combine with _.map when normalizing arrays of records
❌ Don’t
Expect empty string to become the fallback automatically
Replace || when you intentionally want all falsy values defaulted
Nest defaultTo calls—use one clear expression per value
Confuse defaultTo with _.constant or _.defaults
Use defaultTo for deep nested object merging
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.defaultTo()
Use these points when choosing fallback logic.
5
Core concepts
📊01
Nullish
null / undefined only.
Basics
🔢02
Keep 0
Zero stays zero.
Critical
📝03
Keep ""
Empty string OK.
Pitfall
🔄04
?? twin
Same as coalescing.
Native
→05
defaults
For object merge.
Related
❓ Frequently Asked Questions
_.defaultTo(value, defaultValue) returns value when it is not null and not undefined. If value is null or undefined, it returns defaultValue instead. It is a concise nullish fallback helper.
No. Only null and undefined trigger the fallback. An empty string, 0, false, and NaN are valid values and are returned as-is. Use logical OR (||) only when you intend to replace all falsy values.
The || operator replaces any falsy value (0, '', false, null, undefined, NaN). _.defaultTo() replaces only null and undefined—so 0 and '' are preserved, which is usually safer for user input and numeric settings.
They behave the same for null and undefined. ?? is native ES2020 syntax: value ?? default. _.defaultTo(value, default) is the Lodash equivalent and reads well in lodash-heavy pipelines.
_.defaultTo() works on a single value. _.defaults() merges objects, filling missing undefined properties on a destination from source objects. Use defaultTo per field; use defaults for whole object templates.
_.defaultTo() picks between two values based on nullish check. _.constant() returns a function that always returns one fixed value. defaultTo is for data fallbacks; constant is for function factories.
Did you know?
Before ES2020’s ?? operator, _.defaultTo() was a popular way to avoid the || trap where 0 and "" were accidentally replaced. In new code you can use either—behavior matches for null and undefined.