Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Copy All Properties from One Object to Another Object in JavaScript</title> </head> <body> <h2>returnedTarget object</h2> <pre id="returnedTarget_output"></pre> <h2>target object</h2> <pre id="target_output"></pre> <script> // 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); document.getElementById("returnedTarget_output").innerHTML = JSON.stringify(returnedTarget); // Prints: {a: "Apple", b: "Ball", x: 1, y: 4} document.getElementById("target_output").innerHTML = JSON.stringify(target); // Prints: {a: "Apple", b: "Ball", x: 1, y: 4} </script> </body> </html>