Lodash _.defaults() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Assigns source properties to the destination for keys that are still undefined. Sources apply left to right; once a property is set, later values for that key are ignored. Since Lodash 0.1.0.

Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.

Syntax

Call _.defaults. Start with this line.

Lodash
_.defaults(object, ...sources)
CallWhat it does
_.defaults(object, ...sources)Primary signature.
_.defaults(object)Work with an object’s keys, values, or paths.

Prefer import defaults from "lodash/defaults" when you want a smaller bundle.

Library declarations
Lodash
_.defaults(object, ...sources)

Parameters

ParameterDescription
objectRequired. Destination (mutated and returned)
...sourcesOptional. Default providers (left → right)

Return Value

Return ValueDescription
*The value returned by _.defaults().

Lodash Examples

Example 1 — Official: Keep Defined, Fill Missing

Existing keys win; new keys come from sources.

Lodash
_.defaults({ a: 1 }, { b: 2 }, { a: 3 });
// → { a: 1, b: 2 }
Try It Yourself

How It Works

  1. Destination already has a: 1.
  2. { b: 2 } fills the missing b.
  3. { a: 3 } cannot overwrite a.
  4. Result. { a: 1, b: 2 }.

Example 2 — vs _.assign

Defaults fill gaps; assign overwrites.

Lodash
_.defaults({ a: 1 }, { a: 3, b: 2 });
// → { a: 1, b: 2 }

_.assign({ a: 1 }, { a: 3, b: 2 });
// → { a: 3, b: 2 }
Try It Yourself

How It Works

  1. defaults. Keeps destination a: 1; adds b.
  2. assign. Later source wins → a becomes 3.
  3. Both add missing b: 2.
  4. Pick defaults for option bags; assign for merges that overwrite.

Example 3 — null vs undefined

Only undefined is treated as a gap.

Lodash
_.defaults({ a: undefined, b: null }, { a: 1, b: 2, c: 3 });
// → { a: 1, b: null, c: 3 }
Try It Yourself

How It Works

  1. a is undefined → filled with 1.
  2. b is null → kept (not a gap).
  3. c is missing → filled with 3.
  4. Tip. Normalize null first if you want it replaced too.

Example 4 — Safe Options Pattern

Merge user options onto a fresh object with factory defaults.

Lodash
var factory = { retries: 3, timeout: 1000, debug: false };
var user = { timeout: 5000 };

var opts = _.defaults({}, user, factory);
// → { timeout: 5000, retries: 3, debug: false }

user;
// → { timeout: 5000 }  // unchanged
Try It Yourself

How It Works

  1. {} is the destination — nothing is mutated that you care about.
  2. user applies first → timeout: 5000.
  3. factory fills the remaining undefined keys.
  4. Pattern. Classic “options with defaults” helper.

Example 5 — First Source Wins

Among sources, the leftmost defining value sticks.

Lodash
_.defaults({}, { a: 1 }, { a: 2, b: 3 }, { b: 4, c: 5 });
// → { a: 1, b: 3, c: 5 }
Try It Yourself

How It Works

  1. First source sets a: 1.
  2. Second cannot change a; sets b: 3.
  3. Third cannot change b; sets c: 5.
  4. Order matters — put higher-priority defaults earlier.

Browser & Runtime Support

_.defaults is a Lodash Object helper (since 0.1.0). It runs anywhere Lodash 4.x runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.1.0+

_.defaults()

Available in [email protected] full builds and the lodash/defaults modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.defaults Lodash 0.1.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import defaults from "lodash/defaults". Use defaultsDeep when nested objects need gap-filling too.

Frequently Asked Questions

_.defaults(object, ...sources) assigns source properties to the destination only where the destination value is undefined. Sources apply left to right; once a key is set, later sources are ignored for that key.
_.assign overwrites — later sources win. _.defaults only fills undefined gaps — the destination (and the first defining source) win.
No. Only undefined counts as missing. null is treated as already set.
Yes. Pass {} when you want a fresh object: _.defaults({}, userOpts, factoryDefaults).
No. Nested objects already present on the destination are left alone. Use _.defaultsDeep for recursive gap-filling.
Use import defaults from "lodash/defaults" or require("lodash/defaults"). Full build: import _ from "lodash" then _.defaults.

Next: _.defaultsDeep()

Recursively fill undefined nested properties.

_.defaultsDeep() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful