ECMAScript 2018 Standard Method

You would use object spread:

let merged = {...obj1, ...obj2};

merged is now the union of obj1 and obj2. Properties in obj2 will overwrite those in obj1.

/** There's no limit to the number of objects you can merge.
 *  Later properties overwrite earlier properties with the same name. */
const allRules = {...obj1, ...obj2, ...obj3};

Here is also the MDN documentation for this syntax. If you're using babel you'll need the @babel/plugin-proposal-object-rest-spread plugin for it to work (This plugin is included in @babel/preset-env, in ES2018).

ECMAScript 2015 (ES6) Standard Method

/* For the case in question, you would do: */
Object.assign(obj1, obj2);

/** There's no limit to the number of objects you can merge.
 *  All objects get merged into the first object. 
 *  Only the object in the first argument is mutated and returned.
 *  Later properties overwrite earlier properties with the same name. */
const allRules = Object.assign({}, obj1, obj2, obj3, etc);

(see MDN JavaScript Reference)


Method for ES5 and Earlier

for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }

Note that this will simply add all attributes of obj2 to obj1 which might not be what you want if you still want to use the unmodified obj1.

If you're using a framework that craps all over your prototypes then you have to get fancier with checks like hasOwnProperty, but that code will work for 99% of cases.

Example function:

/**
 * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
 * @param obj1
 * @param obj2
 * @returns obj3 a new object based on obj1 and obj2
 */
function merge_options(obj1,obj2){
    var obj3 = {};
    for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
    for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
    return obj3;
}

additionally :- check this program for see differnce between Object.assign & spread syntax object literals

Top answer
1 of 16
4198

ECMAScript 2018 Standard Method

You would use object spread:

let merged = {...obj1, ...obj2};

merged is now the union of obj1 and obj2. Properties in obj2 will overwrite those in obj1.

/** There's no limit to the number of objects you can merge.
 *  Later properties overwrite earlier properties with the same name. */
const allRules = {...obj1, ...obj2, ...obj3};

Here is also the MDN documentation for this syntax. If you're using babel you'll need the @babel/plugin-proposal-object-rest-spread plugin for it to work (This plugin is included in @babel/preset-env, in ES2018).

ECMAScript 2015 (ES6) Standard Method

/* For the case in question, you would do: */
Object.assign(obj1, obj2);

/** There's no limit to the number of objects you can merge.
 *  All objects get merged into the first object. 
 *  Only the object in the first argument is mutated and returned.
 *  Later properties overwrite earlier properties with the same name. */
const allRules = Object.assign({}, obj1, obj2, obj3, etc);

(see MDN JavaScript Reference)


Method for ES5 and Earlier

for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }

Note that this will simply add all attributes of obj2 to obj1 which might not be what you want if you still want to use the unmodified obj1.

If you're using a framework that craps all over your prototypes then you have to get fancier with checks like hasOwnProperty, but that code will work for 99% of cases.

Example function:

/**
 * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
 * @param obj1
 * @param obj2
 * @returns obj3 a new object based on obj1 and obj2
 */
function merge_options(obj1,obj2){
    var obj3 = {};
    for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
    for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
    return obj3;
}

additionally :- check this program for see differnce between Object.assign & spread syntax object literals

2 of 16
1245

jQuery also has a utility for this: http://api.jquery.com/jQuery.extend/.

Taken from the jQuery documentation:

// Merge options object into settings object
var settings = { validate: false, limit: 5, name: "foo" };
var options  = { validate: true, name: "bar" };
jQuery.extend(settings, options);

// Now the content of settings object is the following:
// { validate: true, limit: 5, name: "bar" }

The above code will mutate the existing object named settings.


If you want to create a new object without modifying either argument, use this:

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);

// The content of settings variable is now the following:
// {validate: true, limit: 5, name: "bar"}
// The 'defaults' and 'options' variables remained the same.
🌐
SitePoint
sitepoint.com › blog › javascript › how to merge objects in javascript
How to Merge Objects in JavaScript — SitePoint
November 5, 2024 - The spread operator (...) is a common approach to merge objects in JavaScript. It has the form {...object1, ...object2}. When properties with the same keys exist in the source objects, the spread operator overwrites the values in the target object with the latest source object’s values.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › merge-two-objects
How to Merge Two Objects in JavaScript - Mastering JS
April 29, 2022 - To merge two objects in JavaScript, you can use the spread ... operator. The spread operator creates a new object with all the properties from the first and second object. If there's two properties with the same name, the property from the second ...
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript merge objects
How to Merge Objects in JavaScript
March 29, 2020 - let job = { jobTitle: 'JavaScript Developer', country: 'USA' }; let location = { city: 'London', country: 'England' }; let remoteJob = { ...job, ...location }; console.log(remoteJob); Copy ... In this example, the job and location has the same property country. When we merged these objects, the result object (remoteJob) has the country property with the value from the second object (location). The Object.assign() method allows you to copy all enumerable own properties from one or more source objects to a target object, and return the target object:
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-merge-two-objects-with-same-properties-in-javascript-39a70c0175ac
How to merge two objects with same properties in JavaScript ? | by Pravin M | JavaScript in Plain English
October 9, 2024 - Here are the most commonly used approaches: ... Let’s delve into each method. The Object.assign() method copies all enumerable properties from one or more source objects to a target object. If there are properties with the same name, the values from the later objects will overwrite those ...
🌐
CoreUI
coreui.io › blog › how-to-merge-objects-in-javascript
How to Merge Objects in JavaScript · CoreUI
February 27, 2024 - Object.assign() is another popular choice for shallow merging. This method copies the properties from one or more source objects to a target object, which it returns. Like the spread operator, it overwrites properties in the target object with those from the source objects if they share the same name.
🌐
Flavio Copes
flaviocopes.com › home › javascript › how to merge two objects in javascript
How to merge two objects in JavaScript - Flavio Copes
September 18, 2026 - The best solution in this case is to use Lodash and its merge() method, which will perform a deeper merge, recursively merging object properties and arrays: import merge from 'lodash.merge' const merged = merge({}, person, update) // { name: ...
Find elsewhere
🌐
Scaler
scaler.com › home › topics › javascript merge objects
JavaScript Merge Objects - Scaler Topics
May 4, 2023 - We can use the javascript's pre-defined Object.assign() method to merge two JavaScript objects. What it does is, it copies all enumerable own properties of a given object into a single object and then returns it.
🌐
Programiz
programiz.com › javascript › examples › merge-property-objects
JavaScript Program to Merge Property of Two Objects
The Object.assign() method returns an object by copying the values of all enumerable properties from one or more source objects. // program to merge property of two objects // object 1 const person = { name: 'Jack', age:26 } // object 2 const student = { gender: 'male' } // merge two objects const newObj = {...person, ...student}; console.log(newObj); ... In the above example, two objects are merged together using the spread operator ...
🌐
Byby
byby.dev › js-merge-objects
5 ways to merge two objects in JavaScript
You can use the spread operator to merge two or more objects in JavaScript by creating a new object that contains all the properties from the source objects. If there are properties with the same name in the source objects, the spread operator ...
🌐
Vultr Docs
docs.vultr.com › javascript › examples › merge-property-of-two-objects
JavaScript Program to Merge Property of Two Objects | Vultr Docs
December 17, 2024 - ... This technique creates a new object entirely, ensuring that neither object1 nor object2 is modified during the process. Use Object.assign() to combine the properties of two objects into a new object.
🌐
MSR
rajamsr.com › home › javascript merge objects: the ultimate guide with 17 examples
JavaScript Merge Objects: The Ultimate Guide With 17 Examples | MSR - Web Dev Simplified
December 20, 2023 - The mergedObject contains all the properties of object1 and object2. If a property exists in both objects, the value from object2 overwrites the value from object1. For instance, the age property in mergedObject has the value from object2, not object1. When merging objects with the same keys, the default behavior is to overwrite existing key values.
🌐
Medium
frontendinterviewquestions.medium.com › merge-two-objects-in-javascript-40f16605c13c
Merge two objects in JavaScript. Source:- Merge two objects in… | by Pravin M | Medium
April 15, 2024 - Here's how you can use Object.assign() to merge two objects: const obj1 = { a: 1, b: 2 }; const obj2 = { b: 3, c: 4 }… ... I am a frontend developer with 10+ years of experience Blog :- https://www.frontendinterviewquestions.com
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-merge-the-properties-of-two-javascript-objects.php
How to Merge the Properties of Two JavaScript Objects
// Sample objects var target = { a: "Apple", b: "Ball" }; var source = { x: 1, y: 4 }; // Merging source object into target object var returnedTarget = Object.assign(target, source); console.log(returnedTarget); // Prints: {a: "Apple", b: "Ball", x: 1, y: 4} console.log(target); // Prints: {a: "Apple", b: "Ball", x: 1, y: 4} Please note that only the first object in the list of arguments will be modified and returned. Also, later properties will overwrite earlier properties if they have the same name. See the tutorial on JavaScript Objects to learn more about creating and manipulating objects.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-merge-two-javascript-objects
How to merge two JavaScript objects?
March 15, 2026 - <!DOCTYPE html> <html lang="en"> ... ${JSON.stringify(merged)}</p>`; } </script> </body> </html> When objects have the same property names, the last object's values take precedence:...
🌐
Stack Abuse
stackabuse.com › merge-properties-of-two-objects-dynamically-in-javascript
Merge Properties of Two Objects Dynamically in JavaScript
October 4, 2023 - We can see that the reference to the location object in both person and employee object is the same. In fact, both the spread operator (...) and Object.assign() perform a shallow merge. JavaScript has no deep merge support out of the box. However, there are third-party modules and libraries which do support it, like Lodash's _.merge. In this tutorial, we've taken a look at how to merge two objects in JavaScript.
🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › how-to-merge-two-objects-in-javascript
How to merge two objects in JavaScript | Atomized Objects
Whichever property comes last will overwrite any property with the same key before it. To merge two objects without overwriting in JavaScript is a little similar to the manual deep merge. If you have a nested object but you don’t want to overwrite the property you just need to spread the two properties into a new version of the property.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › object › merge objects
Merge two or more JavaScript objects - 30 seconds of code
March 20, 2024 - const obj1 = { a: 1, b: 2 }; const ... their values. This can be done by checking if the key already exists in the resulting object and appending the value to an array if it does....
🌐
JavaScript in Plain English
javascript.plainenglish.io › merging-javascript-objects-a-deep-dive-bbb00dc51cba
Merging JavaScript Objects: A Deep Dive | by Yogesh Datir | JavaScript in Plain English
December 9, 2024 - Shallow Merge: - Faster as it only copies top-level properties. - Suitable for simple objects without nested structures. - Use Object.assign() or the spread operator for efficient shallow merging. Deep Merge: - Slower due to recursive copying of nested objects.