Underscore.js _.extend() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Shallowly copy all of the properties in the source objects over to the destination object, and return the destination. It is in-order, so the last source overrides properties of the same name in previous arguments (official Underscore docs). Stable Object helper — not deprecated.

Pass the required arguments shown below. Underscore object helpers usually return a new value — check whether the source object is mutated.

Syntax

Call _.extend. Start with this line.

Underscore
_.extend(destination, *sources)
CallWhat it does
_.extend(destination, *sources)Primary signature.
_.extend(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.extend(destination, *sources)

Parameters

ParameterDescription
destinationRequired. Target object (mutated and returned)
*sourcesRequired. One or more objects to copy from

Return Value

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

Underscore.js Examples

Example 1 — Merge Two Objects

Official-style merge: copy source properties onto the destination.

Underscore
var person = { name: "moe" };

_.extend(person, { age: 50 });

person;
// → { name: "moe", age: 50 }
Try It Yourself

How It Works

  1. Target. person is the destination.
  2. Copy. age is assigned onto person.
  3. Keep. Existing name stays.
  4. Return. Same object is mutated and returned.

Example 2 — Last Source Wins

When keys clash, the later source’s value is kept.

Underscore
var result = _.extend({}, { x: 1, y: 1 }, { y: 2, z: 3 });

result;
// → { x: 1, y: 2, z: 3 }
Try It Yourself

How It Works

  1. Fresh target. Start with {}.
  2. First source. Sets x: 1 and y: 1.
  3. Second source. Overwrites y to 2, adds z.
  4. Rule. Left → right; last write wins.

Example 3 — Merge Many Sources

Pass as many source objects as you like.

Underscore
var all = _.extend({}, { a: 1 }, { b: 2 }, { c: 3 });

all;
// → { a: 1, b: 2, c: 3 }
Try It Yourself

How It Works

  1. Empty start. Destination is a new object.
  2. Chain. Each source adds its keys in order.
  3. Combine. Result holds every key from all sources.
  4. Sources. Original source objects stay unchanged.

Example 4 — Merge Without Mutating

Start from {} so existing objects stay intact.

Underscore
var original = { id: 1, tags: ["a"] };

var copy = _.extend({}, original, { id: 2 });

copy;           // → { id: 2, tags: ["a"] }
original.id;    // → 1
Try It Yourself

How It Works

  1. Fresh target. Only {} is mutated.
  2. Copy then override. original is copied, then id becomes 2.
  3. Safe top-level. original.id stays 1.
  4. Shallow note. tags is still the same array reference.

Example 5 — Defaults Plus Overrides

Classic options merge: put defaults first, user options last.

Underscore
function render(userOptions) {
  var defaults = { color: "black", size: 12, bold: false };
  return _.extend({}, defaults, userOptions);
}

render({ size: 16, bold: true });
// → { color: "black", size: 16, bold: true }
Try It Yourself

How It Works

  1. Base. Copy defaults into a new object.
  2. Override. User options overwrite matching keys.
  3. Keep rest. Unmentioned defaults (like color) remain.
  4. vs defaults(). Here later values always win — including overwriting with false/0.

Browser & Runtime Support

_.extend ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore. Similar job to native Object.assign (own props).

✓ Stable · Underscore 1.x

_.extend()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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
_.extend Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.extend(destination, *sources) shallowly copies properties from one or more sources into destination and returns destination. Later sources overwrite earlier ones for the same key.
Yes. It mutates the destination in place and returns it. Sources are not changed. Use _.extend({}, a, b) to avoid mutating an existing object.
_.extend copies enumerable properties, including inherited ones. _.extendOwn (alias _.assign) copies only own properties.
_.extend overwrites existing keys. _.defaults only fills keys that are undefined and never clobbers set values.
Shallow. Nested objects and arrays are copied by reference, not duplicated.
Merging configs, applying overrides on defaults, mixing properties into a target, or combining several plain objects in one call.

Next: _.extendOwn()

Copy only own properties onto a destination (alias _.assign).

_.extendOwn() tutorial →

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.

14 people found this page helpful