How to call function from it name stored in a string using JavaScript ? Last Updated : 26 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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 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: This example explains the above-used approach. html <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> Output: 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: This example explains the above-used approach. html <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> Output: Comment More infoAdvertise with us sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies javascript-functions JavaScript-Questions Similar Reads setTimeout() in JavaScript The setTimeout() function is used to add delay or scheduling the execution of a specific function after a certain period. It's a key feature of both browser environments and Node.js, enabling asynchronous behavior in code execution.JavaScriptsetTimeout(function() { console.log('Hello, world!'); }, 2 2 min read What is a typical use case for anonymous functions in JavaScript ? In this article, we will try to understand what exactly an Anonymous function is, and how we could declare it using the syntax provided in JavaScript further we will see some examples (use-cases) where we can use anonymous functions to get our results in the console. Before proceeding with the examp 4 min read How to check whether a number is NaN or finite in JavaScript ? When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN 3 min read How to Encode and Decode a URL in JavaScript? Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how 4 min read How to declare the optional function parameters in JavaScript ? Declaring optional function parameters in JavaScript means defining function parameters that aren't required when the function is called. You can assign default values to these parameters using the = syntax, so if no argument is provided, the default value is used instead. These are the following ap 3 min read How to get the javascript function parameter names/values dynamically ? In this article, we are given any arbitrary JavaScript function and the task is to return the parameter names of the function. Approach: JavaScript contains a method called toString() which is used to represent a function code in its string representation. This method is used to get the parameter na 2 min read How To Include a JavaScript File in Another JavaScript File? The import and export syntax available in ES6 (ECMAScript 2015) module is used to include a JavaScript file in another JavaScript file. This approach is the most modern and recommended way to share code between JavaScript files.It allows you to break your code into smaller modules and then import th 4 min read How to override a JavaScript function ? In this article, we are given an HTML document and the task is to override the function, either a predefined function or a user-defined function using JavaScript. Approach: When we run the script then Fun() function is called. After clicking the button the GFG_Fun() function is called and this funct 2 min read Using the function* Declaration in JavaScript The function* declaration is used to define a generator that returns a Generator object. Generators are very powerful for asynchronous programming as they aim to resolve callback problems. In a generator, the yield keyword is used instead of return. The yield statement suspends the functionâs execut 2 min read What is the difference between call and apply in JavaScript ? JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr 2 min read Like