How to call function from it name stored in a string using JavaScript?
There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.
Method 1: Using the window object: The window object in HTML 5 references the current window and all items contained in it. Hence we can use it to run a function in a string along with parameters to the function.
Syntax:
window[functionName](parameters)
Example:
<!DOCTYPE html> <html> <head> <title> How to call function from string stored in a variable using JavaScript </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to call function from string stored in a variable using JavaScript </b> <p> Click on the button to call the function in the string </p> <p class="example"> GeeksforGeeks is a computer science portal. </p> <button onclick="evaluateFunction()"> Click here </button> <script type="text/javascript"> function changeColor(color) { document.querySelector('.example').style = `color: ${color}`; } function evaluateFunction() { stringFunction = "changeColor"; param = 'green'; window[stringFunction](param); } </script> </body> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Method 2: Using the eval() method: The other method that can be used is the eval() method. The string that may be passed on to the function may include a JavaScript expression, statement, or a sequence of statements. It can also have variables and properties of existing objects. The only problem with this method is that it is considered unsafe and may not be supported by newer browsers.
Syntax:
eval(stringFunction)
Example:
<!DOCTYPE html> <html> <head> <title> How to call function from string stored in a variable using JavaScript </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to call function from string stored in a variable using JavaScript </b> <p> Click on the button to call the function in the string </p> <p class="example"> GeeksforGeeks is a computer science portal. </p> <button onclick="evaluateFunction()"> Click here </button> <script type="text/javascript"> function changeColor(color) { document.querySelector('.example').style = `color: ${color}`; } function evaluateFunction() { stringFunction = "changeColor('green')"; eval(stringFunction); } </script> </body> </html> |
Output:
- Before clicking the button:

- After clicking the button:

Recommended Posts:
- How to call PHP function from string stored in a Variable
- JavaScript | Function Call
- JavaScript | Call a function after a fixed time
- How to call a function that return another function in JavaScript ?
- What is the difference between call and apply in JavaScript?
- Call multiple JavaScript functions in onclick event
- JavaScript | String() Function
- Difference between Call by Value and Call by Reference
- How to call PHP function on the click of a Button ?
- How to create a function from a string in JavaScript ?
- Call a parent window function from an iframe
- How to remove error Call to undefined function curl_init()?
- How to call a function automatically after waiting for some time using jQuery?
- JavaScript | Pass string parameter in onClick function
- JavaScript | Check if a string is a valid JSON string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



