How to execute a function when its name as a string in JavaScript ?
Last Updated :
09 Jan, 2024
To execute a function when its name is a string in JavaScript, we have multiple approaches. In this article, we are going to learn how to execute a function when its name is a string in JavaScript.
Example:
function myFunction() {
...
}
const functionName ="myFunction";
Below are the approaches used to execute a function when its name is a string in JavaScript:
Approach 1: Using eval() Method
The eval() method is a powerful but often discouraged feature of JavaScript that allows you to execute arbitrary code as a string. You can use eval() to execute a JavaScript function when you have its name as a string.
Syntax:
function myFunction() {
...
}
const functionName = "myFunction";
eval(functionName + "()");
Example: In this example, we define a function greet that logs a greeting to the console, and a variable functionName with the value “greet”, which is the name of the function. Then we use eval() method to execute the function by constructing a string of code that calls the function by name, passing in the argument “Alice”. This results in the greet function being executed, and the output “Hello, Alice!” being logged to the console.
Javascript
function greet(name) {
console.log(`Hello, ${name}!`);
}
const functionName = "greet" ;
eval(`${functionName}( "Alice" )`);
|
Approach 2: Using window[]
In a browser environment, you can use the window[] syntax to execute a JavaScript function when you have its name as a string.
Syntax:
function myFunction() {
...
}
const functionName = "myFunction";
window[functionName]();
Example: In this example, we define a function greet that logs a greeting to the console, and a variable functionName with the value “greet”, which is the name of the function. We then use window[functionName] to access the function by its name as a string, and call it with the argument “Alice”. This results in the greet function being executed, and the output “Hello, Alice!” being logged to the console.
Javascript
function greet(name) {
console.log(`Hello, ${name}!`);
}
const functionName = "greet" ;
window[functionName]( "Alice" );
|
Output:
Hello, Alice!
You can create a new function object using the Function constructor, passing in the function name as a string, and then call that new function object.
Syntax:
function myFunction() {
...
}
const functionName = "myFunction";
const fn = new Function(`return ${functionName}()`);
fn();
Example: In this example, we define three variables: functionName with the value “greet”, which is the name of the function, functionArguments with the value “name”, which is the name of the function’s argument, and functionBody with the value “console.log(‘Hello, ‘ + name + ‘!’);” which is the body of the function. We then use the Function constructor to create a new function dynamically with the given arguments and body and store it in a variable dynamicFunction.
Javascript
const functionName = "greet" ;
const functionArguments = "name" ;
const functionBody =
`console.log( 'Hello, ' + name + '!' );`;
const dynamicFunction =
new Function(functionArguments, functionBody);
dynamicFunction( "John" );
|
Similar Reads
How to call function from it name stored in a string using JavaScript ?
In this article, we will call a function from the string stored in a variable. There are two methods to call a function from a string stored in a variable. Using window object methodUsing eval() method Note: The eval() method is older and is deprecated. Method 1: Using the window object. The window
2 min read
How to create a function from a string in JavaScript ?
The task is to create a function from the string given in the format of the function. Here are a few approaches that are listed below: Using Function() ConstructorUsing eval() MethodApproach 1: Using Function() ConstructorUse the Function() Constructor to create a function from the string.It accepts
2 min read
How to transform String into a function in JavaScript ?
In this article, we will see how to transform a String into a function in JavaScript. There are two ways to transform a String into a function in JavaScript. The first and easy one is eval() but it is not a secure method as it can run inside your application without permission hence more prone to th
3 min read
How to run a function when the page is loaded in JavaScript ?
A function can be executed when the page loads successfully. This can be used for various purposes like checking for cookies or setting the correct version of the page depending on the user's browser. Below are the approaches to run a function when the page is loaded in JavaScript: Table of Content
2 min read
How to Check Whether a String Contains a Substring in JavaScript?
Given two strings, check if one string is substring of another. "bce" is substring of "abcde""ae" is not substring of "abcde"Empty String is a substring of all stringsUsing includes() - Most used and Simplest MethodThe includes() method checks whether a string contains a substring. [GFGTABS] JavaScr
2 min read
How to Call a JavaScript Function using an onsubmit Event ?
HTML has a form element that is used to get the data from the user and send the data to the server. Before the HTML form sends data to the server, it generates an event called "submit". This event can be utilized to execute a JavaScript function that can either validate the form data or notify the u
3 min read
How to run a function in a separate thread by using a Web Worker in JavaScript ?
JavaScript is a popular lightweight, interpreted compiled client-side scripting language. Most Web Applications use JavaScript on the client side. By providing JavaScript with a runtime environment, it can also be used on the server-side (Node.js). In this article, we will cover how to run a functio
3 min read
How to get currently running function name using JavaScript ?
Given a function and the task is to get the name of function that is currently running using JavaScript. Approach 1: Using arguments.callee method: It refers to the currently executing function inside the function body of that function. In this method, we use arguments.callee to refer to the name of
2 min read
Passing a function as a parameter in JavaScript
In this article, we will pass a function as a parameter in JavaScript. Passing a function as an argument to the function is quite similar to passing a variable as an argument to the function. So variables can be returned from a function. The below examples describe passing a function as a parameter
1 min read
How to check a string data type is present in array using JavaScript ?
In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator retu
3 min read