jQuery.extend()

Merge the contents of two or more objects together into the first object.

jQuery.extend(target, object1, objectN)🡢 Object

target ObjectAn object that will receive the new properties.
object1 ObjectAn object containing additional properties to merge in.
objectN ObjectAdditional objects containing properties to merge in.

jQuery.extend(deep, target, object1, objectN)🡢 Object

deep BooleanIf true, the merge becomes recursive (aka. deep copy). Passing false for this argument is not supported.
target ObjectThe object to extend. It will receive the new properties.
object1 ObjectAn object containing additional properties to merge in.
objectN ObjectAdditional objects containing properties to merge in.

jQuery.extend(object)🡢 Object

object ObjectAn object to merge onto the jQuery namespace.

When two or more object arguments are supplied to $.extend(), properties from all of the objects are added to the target object. Arguments that are null or undefined are ignored.

If only one argument is supplied to $.extend(), this means the target argument was omitted. In this case, the jQuery object itself is assumed to be the target. By doing this, you can add new functions to the jQuery namespace. This can be useful for plugin authors wishing to add new methods to JQuery.

Keep in mind that the target object (first argument) will be modified, and will also be returned from $.extend(). If, however, you want to preserve both of the original objects, you can do so by passing an empty object as the target:

var object = $.extend({}, object1, object2);

The merge performed by $.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing true for the first function argument, objects will be recursively merged.

Warning: Versions prior to 3.4 had a security issue where calling jQuery.extend(true, {}, object) on an unsanitized object containing a __proto__ property would extend Object.prototype.

Warning: Passing false for the first argument is not supported.

Undefined properties are not copied. However, properties inherited from the object's prototype will be copied over. Properties that are an object constructed via new MyCustomObject(args), or built-in JavaScript types such as Date or RegExp, are not re-constructed and will appear as plain Objects in the resulting object or array.

On a deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not. Deep-extending a cyclical data structure will result in an error.

For needs that fall outside of this behavior, write a custom extend method instead, or use a library like lodash.

Merge two objects, modifying the first.

JS
<div id="log"></div>
HTML
var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97,
};
var object2 = {
  banana: { price: 200 },
  durian: 100,
};

// Merge object2 into object1
$.extend(object1, object2);

// Assuming JSON.stringify - not available in IE<8
$("#log").append(JSON.stringify(object1));
DEMO

Merge two objects recursively, modifying the first.

JS
<div id="log"></div>
HTML
var object1 = {
  apple: 0,
  banana: { weight: 52, price: 100 },
  cherry: 97,
};
var object2 = {
  banana: { price: 200 },
  durian: 100,
};

// Merge object2 into object1, recursively
$.extend(true, object1, object2);

// Assuming JSON.stringify - not available in IE<8
$("#log").append(JSON.stringify(object1));
DEMO

Merge defaults and options, without modifying the defaults. This is a common plugin development pattern.

JS
<div id="log"></div>
HTML
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };

// Merge defaults and options, without modifying defaults
var settings = $.extend({}, defaults, options);

// Assuming JSON.stringify - not available in IE<8
$("#log").append(
  "<div><b>defaults -- </b>" + JSON.stringify(defaults) + "</div>"
);
$("#log").append(
  "<div><b>options -- </b>" + JSON.stringify(options) + "</div>"
);
$("#log").append(
  "<div><b>settings -- </b>" + JSON.stringify(settings) + "</div>"
);
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)