Create a function with Function constructor in JavaScript

Description

The following code shows how to create a function with Function constructor.

Example


<!--  w w  w  . j a v  a  2 s  .  c om-->
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var sum = new Function("num1", "num2", "return num1 + num2");
document.writeln(sum(1,2));
</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Create a function with Function constructor in JavaScript