JavaScript Function() Constructor Last Updated : 09 Jun, 2024 Comments Improve Suggest changes Like Article Like Report The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions need to be created based on runtime conditions or user input. One notable characteristic of functions created with the Function() constructor is that they have a global scope, meaning they are accessible from anywhere in the code. Syntax: var variable = new Function(arg1, arg2..., Function Body)Parameters: arg*: It is an optional parameter. A function can contain any number of string arguments.Function Body: It is an argument that contains the Javascript statements containing the function definition.Below are some examples that show the use of the Javascript function constructor. Example 1: In this example, we will see the basic use of the Javascript function constructor. HTML <!DOCTYPE HTML> <html> <head> <script type="text/javascript"> <!-- var func = new Function("x", "y", "return x*y;"); function geeks() { var result; result = func(10,20); document.write ( result ); } </script> </head> <body> <h1 style="color:green"> Geeks For Geeks </h1> <h3>Javascript function constructor</h3> <p>Click the following button to see the result</p> <input type="button" onclick="geeks()"value="Click Me"> </body> </html> Output: Javascript function constructorExample 2: In this example, we will create a Javascript constructor and print the values of the function. HTML <!DOCTYPE HTML> <html> <head> <script type="text/javascript"> <!-- var func = new Function("firstName", "lastName", "return firstName + ' ' + lastName + ' ' + firstName;"); function geeks() { var result; result = func("Geeks","for"); document.write ( result ); } </script> </head> <body> <h1 style="color:green"> Geeks For Geeks </h1> <h3>Javascript function constructor</h3> <p>Click the following button to see the result</p> <input type="button" onclick="geeks()"value="Click Me"> </body> </html> Output: Javascript function constructorConclusionThe JavaScript Function() constructor provides a dynamic way to create functions, offering flexibility in scenarios where functions must be generated at runtime. While powerful, it should be used with caution to avoid security and performance issues. Understanding how and when to use the Function() constructor can enhance your JavaScript programming skills, allowing you to create more adaptable and responsive applications. By leveraging this feature effectively, developers can write more dynamic code, making their applications more robust and versatile. We have a complete list of Javascript Functions, to check those please go through this Javascript Function Complete reference article. Comment More infoAdvertise with us Next Article JavaScript Function() Constructor A amitsingh2730 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads Default Constructor in JavaScript In JavaScript, a default constructor is not explicitly defined like in some other programming languages such as Java or C++. In JavaScript, objects can be created without a formal constructor. When you create an object using the new keyword along with a constructor function, that function serves as 2 min read JavaScript Generator() Constructor In JavaScript, there is no particular Generator() constructor but instead, we can use generator function declaration to construct a Generator object which is iterable as the Generator object is a subclass of the Iterable class. Generators are usually used to create functions that can be exited and r 1 min read What is the (function() { } )() construct in JavaScript? If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit 3 min read What is a Constructor in JavaScript? A constructor in JavaScript is a special function that is used to create and initialize objects. When we want to create multiple objects with similar properties and methods, the constructor is used as a blueprint to create similar objects. This is useful when you want to create multiple objects with 8 min read JavaScript Anonymous Functions An anonymous function is simply a function that does not have a name. Unlike named functions, which are declared with a name for easy reference, anonymous functions are usually created for specific tasks and are often assigned to variables or used as arguments for other functions.In JavaScript, you 3 min read JavaScript Function Definitions JavaScript functions are declared using the function keyword, either as a declaration or expression. Declarations define named functions, while expressions assign functions to variables. Both enable code reuse and modularity.SyntaxFunction Declarationsfunction functionName( parameters ) { // Stateme 2 min read JavaScript Symbol() Constructor The Symbol() constructor is used to create a new symbol. The Symbol() constructor returns a value of the type of symbol with static properties. Every time we call the constructor a unique symbol is created. A Symbol constructor is a primitive data type having no object or no methods which are genera 2 min read JavaScript Constructor Method A constructor in JavaScript is a special function used to create and initialize objects. It sets up object properties and is typically invoked using the new keyword. Constructors allow for the creation of multiple instances with similar properties and methods.In JavaScript, constructors can be defin 7 min read JavaScript String() Constructor The JavaScript String() Constructor is used to can be used as a constructor or a function. that creates a new string object. It can be used in two different ways:Syntax:Invoked with the new keyword:new String(object);Invoked without the new keyword:String(object);Parameters: This constructor accepts 3 min read JavaScript Number() Constructor Javascript Number() Constructor is used to create a new number object but, if we call it as a function on another data type it will perform type conversion to number if possible. Syntax: Number(object) Parameters: This function accepts a single parameter as mentioned above and described below: objec 2 min read Like