Categories
JavaScript Answers

How to merge two JavaScript objects together in ES6+?

Spread the love

Sometimes, we want to merge two JavaScript objects together in ES6+.

In this article, we’ll look at how to merge two JavaScript objects together in ES6+.

How to merge two JavaScript objects together in ES6+?

To merge two JavaScript objects together in ES6+, we use the object spread syntax.

For instance, we write

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

to merge the properties of obj1 and obj2 into a new object.

A shallow copy of obj1 and obj2 are made before their contents are put in the new object.

Conclusion

To merge two JavaScript objects together in ES6+, we use the object spread syntax.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *