jQuery.extend() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

jQuery.extend(target, object1, …) (alias $.extend) merges properties into the first object and returns it (since jQuery 1.0). Pass true first for a recursive deep merge (since 1.1.4).

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.extend(target, source)           // shallow — nested objects replaced
$.extend(true, target, source)     // deep — nested plain objects merge
$.extend({}, defaults, options)    // new object; defaults stay intact
$.extend({ myPlugin: fn })         // one arg → extends jQuery itself
CallWhat it does
jQueryLearn jQuery.extend(): shallow and deep object merge, preserve defaults with {}, extend the jQuery namespace, vs Object.assign, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Pick the target (often {} if you want a fresh object).
Step 2Optionally pass true first for a deep merge.
Step 3Pass one or more source objects — later keys overwrite earlier ones.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Merge Two Objects (Shallow)

Official style: modify the first object; nested keys are replaced.

jQuery
var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97
};
var object2 = {
  banana: { price: 200 },
  durian: 100
};

$.extend(object1, object2);
console.log(JSON.stringify(object1));
// {"apple":0,"banana":{"price":200},"cherry":97,"durian":100}
Try It Yourself

How It Works

  1. Target. object1 receives new properties in place.
  2. Replace. banana becomes { price: 200 } — weight is lost.
  3. Add. durian: 100 is copied onto the target.

Example 2 — Recursive Deep Merge

Pass true first so nested plain objects combine.

jQuery
var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97
};
var object2 = {
  banana: { price: 200 },
  durian: 100
};

$.extend(true, object1, object2);
console.log(JSON.stringify(object1));
// {"apple":0,"banana":{"weight":52,"price":200},"cherry":97,"durian":100}
Try It Yourself

How It Works

  1. Deep flag. true enables recursive merging.
  2. Nested merge. banana keeps weight and updates price.
  3. Top-level add. durian is still copied as usual.

Example 3 — Defaults + Options (Plugin Pattern)

Merge into {} so the defaults object stays unchanged.

jQuery
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };

var settings = $.extend({}, defaults, options);

console.log(JSON.stringify(defaults));   // unchanged
console.log(JSON.stringify(options));    // unchanged
console.log(JSON.stringify(settings));
// {"validate":true,"limit":5,"name":"bar"}
Try It Yourself

How It Works

  1. Empty target. {} is the object that gets modified.
  2. Copy defaults first, then overwrite with options.
  3. Result. settings has limit: 5 plus updated flags/name.

Frequently Asked Questions

jQuery.extend(target, object1, …) copies properties from one or more sources into target. The target is modified in place and also returned. Null and undefined sources are ignored.
Default is shallow: a nested object on the target is replaced wholesale by the same key from a source. Pass true first — $.extend(true, target, source) — to recursively merge plain objects and arrays.
Use an empty object as the target: var settings = $.extend({}, defaults, options). defaults stays untouched; settings gets the combined result.
No. undefined values are skipped. Inherited prototype properties on sources can still be copied.
Object.assign() is shallow only and native ES2015+. $.extend() also supports deep merge with true and has long been the plugin pattern for defaults + options in jQuery.
If you pass a single object, jQuery itself is the target — properties are added to the $ / jQuery namespace. Plugin authors use this to register helpers like $.myPlugin.

Next: jQuery.merge() Method

Learn how jQuery.merge appends two array-like collections.

jQuery.merge() Method 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.

8 people found this page helpful