Categories
JavaScript Answers

How to assign a function to the property of a JavaScript object?

Spread the love

Sometimes, we want to assign a function to the property of a JavaScript object.

In this article, we’ll look at how to assign a function to the property of a JavaScript object.

How to assign a function to the property of a JavaScript object?

To assign a function to the property of a JavaScript object, we can use the assignment operator.

For instance, we write:

const obj = {}
obj.hello = () => {
  console.log('hello')
}
obj.hello()

to assign a function that logs 'hello' as the value of the obj.hello property.

Then we call the function with obj.hello().

Conclusion

To assign a function to the property of a JavaScript object, we can use the assignment operator.

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 *