What is the equivalent of Python’s dict.update() in JavaScript?

Sometimes, we want to update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript.

In this article, we’ll look at how to update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript.

What is the equivalent of Python’s dict.update() in JavaScript?

To update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript, we can use the spread operator.

For instance, we write:

const marks = {
  'Physics': 67,
  'Maths': 87
}
const internalMarks = {
  'Practical': 48
}
const newMarks = {
  ...marks,
  ...internalMarks
}
console.log(newMarks)

to merge the marks and internalMarks object into the newMarks object with the spread operator.

As a result, newMarks is {Physics: 67, Maths: 87, Practical: 48}.

Conclusion

To update a JavaScript object with the equivalent of Python’s dict.update() in JavaScript, we can use the spread operator.