How to create a function from a string in JavaScript ? Last Updated : 25 Jul, 2023 Suggest changes Share Like Article Like Report 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 any number of arguments (in the form of string). The last one should be the body of the function.In this example, Only the body of the function is passed, returning a value.Example 1: This example implements the above approach. JavaScript let func = 'return "This is return value";'; // We can use 'func' as function function funFromString() { let func2 = Function(func); // Now 'func' can be used as function console.log(func2()); } funFromString(); OutputThis is return valueApproach 2: Using eval() MethodUse the eval() method to create a function from the string.It accepts the function in the form of a string and converts it to a JavaScript function.In this example, It takes 2 arguments and returns the sum of both numbers.Example 2: This example uses the approach discussed above. JavaScript const str = "var func = function (a, b) { return a + b; };"; // Till this point we can use 'func' as function function funFromString() { // Converting the string to function eval(str); console.log(func(2, 5)); } funFromString(); Output7 Advertise with us Next Article How to create a function from a string in JavaScript ? P PranchalKatiyar Follow Similar Reads How to create an element from a string in JavaScript ? In this article, we will learn how to create an element from a string using JavaScript. This can be used in situations where dynamically generated elements are required by the user. This can be achieved using many approaches as given below: Table of Content Using the createElement() methodUsing the 3 min read How to create hash from string in JavaScript ? To create a unique hash from a specific string, it can be implemented using its own string-to-hash converting function. It will return the hash equivalent of a string. Also, a library named Crypto can be used to generate various types of hashes like SHA1, MD5, SHA256, and many more. These are the fo 3 min read JavaScript - How to Get Character Array from String? Here are the various methods to get character array from a string in JavaScript.1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. JavaScriptlet s = "Geeksf 2 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 How to Call a JavaScript Function from Chrome Console ? One can call a JavaScript Function from the Chrome Console. We will learn how can we call the function in the console that is written in JavaScript. Steps to call a JavaScript Function from Chrome ConsoleOpen the Chrome ConsoleYou can open the Chrome Console by right-clicking on your webpage, select 2 min read Article Tags : JavaScript Web Technologies JavaScript-Questions Like