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 function. Here are few techniques discussed with the help of JavaScript.
Approach 1:
- Use the Function() Constructor to create a function from the string.
- It accepts any number of arguments(in form of string). Last one should be the body of the function.
- In this example, Only the body of the function is passed which is returning a value.
Example 1: This example uses approach as discussed above
<!DOCTYPE HTML> <html> <head> <title> create a function from a string in JavaScript </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> Click Here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var func = 'return "This is return value";'; up.innerHTML = "Click on the button to create "+ "function from string.<br>FUNCTION - '" + func + "'"; // Till this point we can use 'func' as function. function GFG_Fun() { var func2 = Function(func); // Now 'func' can be used as function. down.innerHTML = func2(); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Use the eval() method to create a function from the string.
- It accepts the function in the form of string and converts it to JavaScript function.
- In this example, It takes 2 arguments and returning the sum of both numbers.
Example 2: This example uses approach as discussed above.
<!DOCTYPE HTML> <html> <head> <title> create a function from a string in JavaScript </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> Click Here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var str = "var func = function (a, b) { return a + b; };"; up.innerHTML = "Click on the button to create "+ "function from string.<br>FUNCTION - '" + str + "'"; // Till this point we can use 'func' as function. function GFG_Fun() { // converting the string to function. eval(str); down.innerHTML = func(2, 5); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to create hash from string in JavaScript ?
- Create a string with multiple spaces in JavaScript
- JavaScript | String() Function
- How to call function from it name stored in a string using JavaScript?
- JavaScript | Pass string parameter in onClick function
- How to create a link in JavaScript ?
- How to create a style tag using JavaScript?
- Object.create( ) In JavaScript
- How to create a pop-up to print dialog box using JavaScript?
- How to create a custom callback in JavaScript?
- How to create two dimensional array in JavaScript?
- How to create multi-line strings in JavaScript?
- How to create an image element dynamically using JavaScript ?
- Create a Hoverable Side Navigation with HTML, CSS and JavaScript
- How to create auto-resize textarea using JavaScript/jQuery ?
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.

