Open In App
Related Articles

How to invoke a function without parenthesis using JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Method 1: Using the new operator.

The new operator is used to create an instance of an object which has a constructor function. This constructor function can be used to write our own function and then be invoked by the new operator. The parenthesis is optional when using this operator, hence the function would be invoked even without them. 

Syntax:

new exampleFunction

Example: 

html

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to invoke a function
        without parentheses?
    </b>
    <p class="output"></p>
      
    <script type="text/javascript">
        function exampleFunction() {
            document.querySelector(".output").textContent
                = "The function was called!";
        }
          
        // Using the new operator
        new exampleFunction;
    </script>
</body>

                    

Output: 

using-new

Method 2: Using tagged template literals.

Tagged template literals are a use case of template literals that was introduced in JavaScript ES6. It allows the use of functions directly when using template literals. It can be used with parameters bypassing them in the literals. To invoke a function, its name is written along with back-ticks (` `) to denote it as tagged template literals. 

Syntax:

exampleFunction``

Example: 

html

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to invoke a function
        without parentheses?
    </b>
    <p class="output"></p>
      
    <script type="text/javascript">
        function exampleFunction() {
            document.querySelector(".output").textContent
                    = "The function was called!";
        }
          
        // Using tagged template literals
        exampleFunction``;
  </script>
</body>

                    

Output: 

using-tagged-templates

Method 3: Modifying the toString() method.

The toString() method is invoked whenever a string conversion is necessary. An object can be created which has the toString() method modified to call the required function. This object can be used to call toString() by performing a concatenation with an empty string. It will force the toString() method to be called which will in turn, invoke the specified function. 

Syntax: 

let tempObj = {
    toString: exampleFunction
}

'' + tempObj;

Example: 

html

<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to invoke a function
        without parentheses?
    </b>
    <p class="output"></p>
      
    <script type="text/javascript">
        function exampleFunction() {
            document.querySelector(".output").textContent
                = "The function was called!";
        }
          
        let tempObj = {
        toString: exampleFunction
        }
          
        // Forcing the modified
        // toString function to be invoked
        '' + tempObj;
    </script>
</body>

                    

Output: 

using-toString


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks 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!


Last Updated : 24 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials