How to transform String into a function in JavaScript ? Last Updated : 26 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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 third-party attacks. The second one is Function() which is way more secure than eval(). Javascript eval(): This method takes a string as an argument and converts it into a function. Here the string can be a JavaScript expression, variable, statement, or sequence of statements.Function constructor: Functions created with the Function constructor do not create closures to their creation contexts; they are always created in the global scope. They will only be able to access their own local and global variables when they execute, not variables from the scope in which the Function constructor was created.Example 1: The following code example shows another way to use Function Constructor to convert the string to function. HTML <!DOCTYPE html> <html> <body> <h1 style="color:green"> GeeksforGeeks </h1> <h3> This example shows how we can transform string into a function in javascript using Function Constructor </h3> <p> The code output is : <span id="result"></span> </p> <script> var fnstring = "func"; var fnparams = ["first", "second", "third"]; // find an object var fn = window[fnstring]; // check if object a function? if (typeof fn === "function") { fn.apply(null, fnparams); } function func(a, b, c) { document.getElementById("result").innerHTML = b; } </script> </body> </html> Output: Example 2: The below code example demonstrate different ways to convert a string into a function. HTML <!DOCTYPE html> <html> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> This example shows how we can transform string into a function in javascript </h3> <h3>1. Use of eval()</h3> <p> When string "var func = function(){ return a+b }" is passed in eval() and func() is called, The output is : <span id="result"></span> </p> <h3>2. Use of Function constructor</h3> <p> When strings "x=5", "y=20", "return x*y" is passed in Function constructor and fn() is called, The output is : <span id="func1"></span> </p> <p> When strings "x=5", "y=20", "return x*y" is passed in Function constructor and fn(4,10) is called, The output is : <span id="func2"></span> </p> <script> // use of eval() var a = 100; var b = 200; eval("var func = function(){ return a+b }"); document.getElementById("result").innerHTML = func(); // Use of Function Constructor const fn = new Function("x=5", "y=20", "return x*y"); document.getElementById("func1").innerHTML = fn(); document.getElementById("func2").innerHTML = fn(4, 10); </script> </body> </html> Output: The output shows the transformation of a string into a function. Comment More infoAdvertise with us Next Article How to transform String into a function in JavaScript ? C chiragchouhan163 Follow Improve Article Tags : HTML HTML-Questions JavaScript-Questions Similar Reads How to execute a function when its name as a string in JavaScript ? 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 3 min read How to write a function in JavaScript ? JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod 4 min read How to convert string into float in JavaScript? In this article, we will convert a string into a float in Javascript. We can convert a string into a float in JavaScript by using some methods which are described below: Methods to Concert String into Float:Table of Content Method 1: By using Type Conversion of JavaScriptMethod 2: By using parseFloa 6 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 Convert String to JSON in JavaScript? In JavaScript, converting a string to JSON is important for handling data interchangeably between server and client, parsing external API responses, and storing structured data in applications. Below are the approaches to converting string to JSON in JavaScript: Table of Content Using JSON.parse()Us 2 min read How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used 3 min read How to Convert String to Camel Case in JavaScript? We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin 4 min read 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 Convert a String to an Integer in JavaScript These are the following ways to convert string to an integer in JavaScript:1. Using Number() MethodThe number() method converts a string into an integer number. It works similarly to the unary plus operator. JavaScriptlet s = "100.45"; console.log(typeof Number(s));Outputnumber 2. Using Unary + Oper 2 min read How to Concatenate Strings in JavaScript? Here are the various methods to concatenate strings in JavaScript1. Using Template Literals (Template Strings)Template literals (introduced in ES6) provide a simple way to concatenate strings. With template literals, you can embed expressions inside a string using ${} syntax. This method makes the c 3 min read Like