How to transform String into a function in JavaScript ? Last Updated : 26 Jun, 2022 Suggest changes Share 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. Advertise with us Next Article How to transform String into a function in JavaScript ? C chiragchouhan163 Follow 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 Article Tags : HTML HTML-Questions JavaScript-Questions Like