Lodash _.get: Safe Nested Property Access with Defaults
Use lodash _.get to safely read nested properties and return a default when undefined. Import from lodash-es/get or lodash.get to keep bundles small.
13 Jan 2026, 04:56 UTC

Desired Outcome
Read a deeply nested property without throwing when an intermediate key is missing, and return a configurable fallback when the resolved value is undefined.
Prerequisites
A JavaScript project with a module bundler (Vite, webpack, Rollup, or similar) or a modern browser that supports ES modules. Install one of the following packages:
lodash-es— for tree-shakable ES module imports (bundlers remove unused code)lodash.get— standalone package with no full lodash dependency
Run the install command in your project directory:
npm install lodash-es
Required permissions: write access to package.json and node_modules, which is standard for local development. Risk: importing from lodash instead of lodash-es/get can defeat tree-shaking and inflate client bundles.
Procedure
1. Import only the get function
Use a submodule import so your bundler can exclude the rest of Lodash:
import get from 'lodash-es/get';
Or with the standalone package:
import get from 'lodash.get';
2. Call _.get with an object, path, and default value
The signature is _.get(object, path, [defaultValue]). The path accepts a string such as 'a.b.c' or an array such as ['a', 'b', 'c'].
import get from 'lodash-es/get';
const user = {
profile: {
settings: {
theme: 'dark'
}
}
};
console.log(get(user, 'profile.settings.theme', 'light'));
console.log(get(user, 'profile.settings.locale', 'en'));
console.log(get(user, 'account.billing.plan', 'free'));
Expected check: missing intermediate keys return the default instead of throwing TypeError.
3. Use array paths for keys that contain dots or brackets
String paths treat every dot as a separator. If an object key literally contains a dot, pass an array so Lodash treats the whole segment as one key.
const data = { a: { 'b.c': 1 } };
console.log(get(data, ['a', 'b.c']));
console.log(get(data, 'a.b.c'));
4. Understand default value semantics
The default value is applied only when the resolved value is undefined. An explicitly stored null is returned as null, not replaced by the default.
console.log(get({ a: { b: undefined } }, 'a.b', 'fallback'));
console.log(get({ a: { b: null } }, 'a.b', 'fallback'));
If null should also trigger the fallback, combine the result with nullish coalescing:
const value = get(obj, 'a.b', 'fallback') ?? 'fallback';
Expected Checks
- Run the import and sample calls in your application entry point or a test file.
- Confirm no
TypeErrorappears when intermediate keys are absent. - Inspect bundle output with your bundler's analyzer plugin to verify only the
getmodule is included, not the full Lodash library. - For dotted keys, verify the array form returns the expected value.
_.get vs Optional Chaining
| Scenario | Use _.get | Use Optional Chaining |
|---|---|---|
| Dynamic path from variable | Yes | No |
| Computed runtime keys | Yes | No |
| Static path known at write time | No | Yes |
| Configurable default value | Yes | Limited (pair with ??) |
Optional chaining (obj?.a?.b?.c) is native syntax and generally faster for static paths. _.get is slower than direct property access or optional chaining in tight loops, so reserve it for user-supplied data, deeply uncertain shapes, or cases where a default is essential.
Limitations and Recovery
_.getis read-only. It cannot assign values. Use_.setor direct mutation for writes.- String paths cannot access a literal key named
'a.b'; use['a', 'b.c']instead. nullvalues are not replaced by the default.- If bundle size increases after import, confirm you are not importing from
'lodash'. Switch to'lodash-es/get'or'lodash.get'.
There is no rollback step because _.get performs read-only access and does not change application state.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.