How to Call Function by Name in JavaScript
- Using the Window Object
-
Using
eval
Function - Using Object Literals
- Using Function Constructor
- Conclusion
- FAQ

Understanding how to call a function by its name in JavaScript is a fundamental skill for any developer. It allows for dynamic execution of functions, enhancing flexibility and reusability in your code. Imagine a scenario where you need to invoke different functions based on user input or other conditions. This capability can significantly streamline your code and make it more efficient.
In this article, we will explore various methods to call a function by its name in JavaScript, providing clear examples and explanations along the way. Whether you’re a beginner or an experienced developer, this guide will help you master this essential technique.
Using the Window Object
In JavaScript, all global functions are properties of the global window
object in browsers. Thus, you can call a function by its name using bracket notation on the window
object. This method is straightforward and effective for global functions.
Here’s how you can do it:
javascriptCopyfunction greet() {
return "Hello, World!";
}
const functionName = "greet";
const result = window[functionName]();
console.log(result);
Output:
textCopyHello, World!
In this example, we define a simple function named greet
. We then store the name of the function in a variable called functionName
. By using window[functionName]()
, we dynamically invoke the greet
function. The result is logged to the console, displaying “Hello, World!”. This method is particularly useful when the function name is not known until runtime, allowing for greater flexibility in your code.
Using eval
Function
The eval
function in JavaScript can also be used to call functions by name. However, it’s important to note that eval
can introduce security risks and performance issues, so it should be used judiciously.
Here’s an example of how to use eval
to call a function:
javascriptCopyfunction farewell() {
return "Goodbye, World!";
}
const functionName = "farewell";
const result = eval(functionName + '()');
console.log(result);
Output:
textCopyGoodbye, World!
In this case, we define a function named farewell
. We store its name in a variable called functionName
. Using eval
, we concatenate the function name with parentheses and execute it. The output will be “Goodbye, World!”. While this method works, it’s advised to avoid eval
unless absolutely necessary due to potential security vulnerabilities and debugging difficulties.
Using Object Literals
Another effective way to call a function by name is to use an object literal. By storing your functions as properties of an object, you can easily access and invoke them dynamically. This method is both safe and efficient.
Here’s how you can implement it:
javascriptCopyconst actions = {
greet: function() {
return "Hello, World!";
},
farewell: function() {
return "Goodbye, World!";
}
};
const functionName = "greet";
const result = actions[functionName]();
console.log(result);
Output:
textCopyHello, World!
In this example, we create an object called actions
that contains two methods: greet
and farewell
. By storing the function name in the variable functionName
, we can call the appropriate method using bracket notation. This approach is not only cleaner but also enhances code organization, making it easier to manage your functions.
Using Function Constructor
The Function
constructor allows you to create functions dynamically. This method can also be used to call functions by name, although it is less common.
Here’s how it works:
javascriptCopyconst functionName = "return 'Hello from Function Constructor!';";
const dynamicFunction = new Function(functionName);
const result = dynamicFunction();
console.log(result);
Output:
textCopyHello from Function Constructor!
In this example, we define a string representing a function body and use the Function
constructor to create a new function. We then invoke this dynamic function and log the result. While this method is powerful, it should be used cautiously as it can lead to code that is harder to read and maintain.
Conclusion
Calling a function by its name in JavaScript is a versatile technique that can enhance the functionality and readability of your code. Whether you choose to use the window
object, eval
, object literals, or the Function
constructor, each method has its unique advantages and considerations. By mastering these techniques, you can write more dynamic and flexible JavaScript code that meets your development needs. Remember to choose the method that best fits your specific use case and always prioritize security and performance.
FAQ
-
How can I call a function by its name in JavaScript?
You can call a function by its name using the window object, eval function, or by using object literals. -
Is using eval safe for calling functions?
Using eval can introduce security risks and performance issues, so it should be used sparingly and with caution. -
What is the best method to call a function by name?
Using object literals is often considered the best method due to its clarity, safety, and efficiency. -
Can I call a function defined within another function by its name?
No, functions defined within another function are scoped to that function and cannot be accessed from the global scope.
- What are the performance implications of using eval?
The eval function can lead to slower performance because it forces the JavaScript engine to re-evaluate the code every time it’s called, which can hinder optimization.
My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.
LinkedIn