
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to merge two JavaScript objects?
Following is the code to merge two JavaScript objects together −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Merge two JavaScript objects</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3> Click the above button to merge person and adress object together. </h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let person = { name: "Rohan", age: 22, }; let address = { state: "Delhi", country: "India", }; let mergedObj = { ...person, ...address }; BtnEle.addEventListener("click", () => { for (let i in mergedObj) { resEle.innerHTML += "Key = " + i + " : Value = " + mergedObj[i] + "<br>"; } }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- How to merge properties of two JavaScript Objects dynamically?
- How to merge two different array of objects using JavaScript?
- How to merge two arrays with objects in one in JavaScript?
- How can I merge properties of two JavaScript objects dynamically?
- Merge two objects in JavaScript ignoring undefined values
- How to merge two arrays in JavaScript?
- How can we merge two JSON objects in Java?
- How to merge objects into a single object array with JavaScript?
- How to merge two strings alternatively in JavaScript
- How to compare two JavaScript Date Objects?
- How to compare two objects in JavaScript?
- Merge objects in array with similar key JavaScript
- How to concatenate two JavaScript objects with plain JavaScript ?
- JavaScript program to merge two objects into a single object and adds the values for same keys
- How to compare two JavaScript array objects using jQuery/JavaScript?

Advertisements