Easy
Merging Objects in JavaScript
Description
In JavaScript, you often need to combine multiple objects into a single object.
This is useful when you want to merge configurations, combine data from different sources, or override default values with user-provided values.
In this challenge, you will create a function that takes two objects as input and returns a new object that merges the properties of both objects.
If both objects have the same key, the value from the second object should override the value from the first object.
Example Object Structure:
const obj1 = {name: "Alice",age: 25};const obj2 = {age: 30,occupation: "Software Engineer"};
Your task is to complete the function mergeObjects, which should take two objects as parameters and return a new object that combines their properties.
Example Usage:
const obj1 = {name: "Alice",age: 25};const obj2 = {age: 30,occupation: "Software Engineer"};console.log(mergeObjects(obj1, obj2)); // Expected output: { name: "Alice", age: 30, occupation: "Software Engineer" }
Constraints:
- The input will always be two valid objects.
- The function should return a new object that combines the properties of both input objects.
- If both objects have the same key, the value from the second object should override the value from the first object.
Did you know?
The first-ever website was created by Tim Berners-Lee for the internet.
Test Cases
Input:
{ "age": 25, "name": "Alice" }
Expected Output:
{ "age": 30, "name": "Alice", "occupation": "Software Engineer" }