How to create a function from a string in JavaScript ? Last Updated : 25 Jul, 2023 Comments Improve Suggest changes 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 Comment More infoAdvertise with us Next Article How to create a function from a string in JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions 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 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 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 How to transform String into a function in JavaScript ? 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 th 3 min read How to Create Custom Functions in math.js? Custom Functions in Math.js are nothing but user-defined functions that extend the capabilities of the library. They allow you to implement specific logic or calculations that are not available in the standard Math.js library. In this article, we will learn to create custom functions in Math.js to p 2 min read Like