How to merge properties of two JavaScript objects into one?
Hello everyone! I'm new to JavaScript and came across this task: I have two objects with data, and I need to merge their properties into one object. For example, there is a `user` object with name and age, and an `address` object with city and street. I want to get one object that contains all properties from both objects.
Here's an example code:
```javascript
const user = {
name: 'Ivan',
age: 30
};
const address = {
city: 'Moscow',
street: 'Tverskaya'
};
// How to get an object that looks like this:
// {
// name: 'Ivan',
// age: 30,
// city: 'Moscow',
// street: 'Tverskaya'
// }
```
I tried simply adding objects with the `+` operator, but it doesn't work — it becomes a string. I tried using `Object.assign()`, but I didn't quite understand how to do it correctly.
No answers yet.
Log in to leave an answer
Blogs
Introduction to Go (Golang) in 2025: Where to Start for Beginners