Thursday, December 18, 2025
HomeLanguagesJavascriptJavaScript Function() Constructor

JavaScript Function() Constructor

The Javascript Function Constructor is used to create a new function object using the Function() constructor with the new operator. The Javascript Function Constructor creates a function that has a global scope. 

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 neveropen() {
           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="neveropen()"value="Click Me">
  
</body>
</html>


Output:

Javascript function constructor

Javascript function constructor

Example 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 neveropen() {
           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="neveropen()"value="Click Me">
  
</body>
</html>


Output:

Javascript function constructor

Javascript function constructor

We have a complete list of Javascript Functions, to check those please go through this Javascript Function Complete reference article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
108 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12035 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6910 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS