Categories
JavaScript Answers

How to use dynamic function name in JavaScript?

Spread the love

Sometimes, we want to use dynamic function name in JavaScript.

In this article, we’ll look at how to use dynamic function name in JavaScript.

How to use dynamic function name in JavaScript?

To use dynamic function name in JavaScript, we can create an object with the function name.

For instance, we write

const name = "myFn";
const fn = { [name]() {} }[name];

to set the fn variable to a function with the name name.

We put the name method in the object and then we get the method with [name].

Conclusion

To use dynamic function name in JavaScript, we can create an object with the function name.

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 *