JavaScript | Function Invocation

JavaScript Function Invocation is used to executes the function code and it is common to use the term “call a function” instead of “invoke a function”. The code inside a function is executed when the function is invoked.

Syntax:

  • Invoking a Function as a Function:
    function myFunction( var ) {
        return var;
    }
    myFunction( value );
  • Invoking a Function as a Method:
    var myObject = {
        var : value,
        functionName: function () {
            return this.var;
        }
    }
    myObject.functionName();  

Parameters: It contains two parameters as mentioned above and described below:



  • functionName: The functionName method is a function and this function belongs to the object and myObject is the owner of the function.
  • this: The parameter this is the object that owns the JavaScript code and in this case the value of this is myObject.

Example 1: This example use function invocation to add two numbers.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Function Invocation
    </title>
</head>
  
<body style="text-align:center;">
      
    <h2>GeeksForGeeks</h2>
          
    <p>
        Function returns the addition
        of 50 and 60
    </p>
          
    <p id="geeks"></p>
      
    <!-- Script to add two numbers -->
    <script>
        function myFunction(a, b) {
            return a + b;
        }
              
        document.getElementById("geeks").innerHTML
                = window.myFunction(50, 60); 
    </script>
</body>
  
</html>                    

chevron_right


Output:

Example 2: This example use function invocation to concatenate strings.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Function Invocation
    </title>
</head>
  
<body style="text-align:center;">
      
    <h2>GeeksForGeeks</h2>
      
    <p>
        myObject.fullName() will return
        GeeksforGeeks
    </p>
      
    <p id="geeks"></p>
      
    <!-- Script to implement Function Invocation -->
    <script>
        var myObject = {
            firstName:"Geeks",
            middleName:"for",
            lastName: "Geeks",
            fullName: function() {
                return this.firstName + this.middleName
                        + this.lastName;
            }
        }
        document.getElementById("geeks").innerHTML
                = myObject.fullName(); 
    </script>
</body>
  
</html>                    

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.