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

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.