By the end of this tutorial, you’ll use _.result() to read nested values, call object methods safely, and fall back to defaults when data is missing.
01
Path resolution
Read properties with dot paths or key arrays.
02
Auto-invoke
Functions at the path run with the parent as this.
03
Defaults
Return a fallback when the resolved value is undefined.
04
Lazy defaults
Pass a function as defaultValue for computed fallbacks.
05
result vs get
Know when to invoke methods vs plain reads.
06
Production tips
Avoid confusing null, missing paths, and method side effects.
Fundamentals
What Is _.result()?
_.result() resolves a property on an object—including nested paths—and returns the final value. If the value at that path is a function, Lodash calls it with the parent object as this and returns what the function returns. If the resolved value is undefined, the optional defaultValue is used instead.
_.get(user, "greet") returns the function itself. _.result(user, "greet")runs the function and gives you the greeting string.
Reach for _.result() when objects expose computed values as methods, when paths may be missing, or when you want a clean default without manual if checks.
Foundation
📝 Syntax
The signature combines an object, a path, and an optional default:
javascript
_.result(object, path, [defaultValue])
Syntax Rules
object — the object to query.
path — property path as a string ("a.b"), array (["a", "b"]), or single key.
Function at path — invoked with parent object as this; its return value is the result.
undefined resolved — returns defaultValue (if default is a function, it is invoked with parent as this).
Other values — returned as-is (null, 0, strings, etc.).
javascript
import result from "lodash/result";
const user = {
name: "John Doe",
greet() {
return `Hello, my name is ${this.name}.`;
}
};
const userName = result(user, "name");
const greeting = result(user, "greet");
// userName -> "John Doe"
// greeting -> "Hello, my name is John Doe."
Returned when the resolved value is undefined. Can be a plain value or a function (invoked with parent as this).
_.result(cfg, "theme", "light")
resolutionBehavior
If the value at path is a function → call it. If result is undefined → use default. Otherwise return the value.
// null and false are returned as-is
A property that exists but holds a non-function value (like a string) is returned directly—the default is not used unless the resolved value is undefined.
Hands-On
Examples Gallery
Practical _.result() patterns with sample output and interactive Try It Yourself labs.
📚 Getting Started
Read properties and invoke object methods in one call.
Example 1 — Read a property and invoke a method
Get name directly and call greet() with the correct this binding.
javascript
const user = {
name: "John Doe",
age: 30,
greet() {
return `Hello, my name is ${this.name}.`;
}
};
const userName = _.result(user, "name");
const userGreeting = _.result(user, "greet");
console.log(userName);
// -> "John Doe"
console.log(userGreeting);
// -> "Hello, my name is John Doe."
Lodash walks the object using the path string or key array.
Navigate
2
Invoke if function
If the value at the path is a function, call it with the parent as this.
Execute
3
Apply default
If the final resolved value is undefined, return or invoke defaultValue.
Fallback
=
🔍
Value returned
One safe read that handles methods and missing data without extra branching.
Important
📝 Notes
Defaults apply only when the resolved value is undefined, not null.
A string at the path (e.g. greet: "Hello") is returned as-is—the default is not used.
Functions at the path are always invoked; use _.get() if you need the function reference.
Methods run with the parent object as this—same as object.method().
If a method throws, the error propagates; _.result() does not catch it.
For writing nested values, see the next tutorial: _.set().
Wrap Up
Conclusion
_.result() combines safe path resolution, automatic method invocation, and optional defaults in one call. Use it when objects expose behavior as functions or when you want readable fallbacks for missing configuration.
For plain reads without invoking functions, use _.get(). To write values at nested paths, continue to _.set().
Use _.result() for computed properties stored as methods
Pass function defaults when fallback logic is non-trivial
Prefer dot paths or arrays consistently across your codebase
Use _.get() when you intentionally need a function reference
Document which paths may invoke side-effect methods
❌ Don’t
Expect defaults when the value is null (use ?? or explicit checks)
Assume non-function values trigger the default
Call _.result() on paths whose methods have heavy side effects unknowingly
Replace _.get() everywhere—only when invoke/default behavior helps
Forget that invalid intermediate paths return undefined and then the default
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.result()
Use these when reading objects with methods or optional fields.
5
Core concepts
🔍01
Path resolve
Dot or array paths.
Basics
▶️02
Invoke fn
Auto-call methods.
Behavior
📝03
Default
When undefined.
Fallback
🔗04
this
Parent object.
Binding
📋05
vs get
No auto-invoke.
Compare
❓ Frequently Asked Questions
_.result() resolves a property path on an object. If the value is a function, it is called with the parent object as this and the return value is used. If the resolved value is undefined, the optional default is returned instead.
_.get() returns whatever sits at the path—including functions without calling them. _.result() automatically invokes functions found at the path and supports default fallbacks when the resolved value is undefined.
Only when the resolved value is undefined—typically because the path is missing or explicitly set to undefined. If the property exists with any other value (including null, false, or a string), that value is returned.
Yes. If the resolved value is undefined and defaultValue is a function, Lodash invokes defaultValue with the parent object as this and returns its result.
Yes. Pass a dot-path string like profile.name or an array of keys like ["profile", "name"]. Lodash walks the object chain before applying the function-or-default rules.
No for reads. If a method at the path mutates the object when invoked, that is the method's behavior—not _.result() itself.
Did you know?
_.result is the read/invoke counterpart to writing with _.set(). Together they cover the two most common nested-object operations in Lodash—plus _.get() when you want the raw value without calling functions.