Categories
JavaScript Answers

How to dynamically invoke object method from string with JavaScript?

Spread the love

Sometimes, we want to dynamically invoke object method from string with JavaScript.

In this article, we’ll look at how to dynamically invoke object method from string with JavaScript.

How to dynamically invoke object method from string with JavaScript?

To dynamically invoke object method from string with JavaScript, we use square brackets.

For instance, we write

foo[method]();

to call the method method in foo.

method is a string with the method name of the method in the foo object.

Conclusion

To dynamically invoke object method from string with JavaScript, we use square brackets.

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 *