Categories
JavaScript Answers

How to add members to an existing object with JavaScript?

Spread the love

Sometimes, we want to add members to an existing object with JavaScript.

In this article, we’ll look at how to add members to an existing object with JavaScript.

How to add members to an existing object with JavaScript?

To add members to an existing object with JavaScript, we can assign a value to a property in the object.

For instance, we write:

const obj = {
  fn1: () => {}
}
obj.fn2 = () => {};
console.log(obj)

We set obj.fn2 to a function to add fn2 as a property of obj.

Conclusion

To add members to an existing object with JavaScript, we can assign a value to a property in the object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *