how to create a function from a string 1

how to create a function from a string

var addition = Function("a", "b", "return a + b;");
alert(addition(5, 3)); // shows '8'

Here is what the above code is Doing:
1. The Function constructor takes a variable number of arguments. The first argument is a string containing a comma-separated list of argument names. The second and following arguments are strings containing JavaScript code.
2. The Function constructor returns a reference to the newly created function.
3. The newly created function is assigned to the variable addition.
4. The addition function is called with two arguments.
5. The addition function returns the sum of its two arguments.

Similar Posts