The Wayback Machine - https://web.archive.org/web/20240118172136/https://www.geeksforgeeks.org/javascript-function-invocation/
Open In App
Related Articles

JavaScript Function Invocation

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

The JavaScript Function Invocation is used to execute 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: 
let myObject = {
    let : value,
    functionName: function () {
        return this.let;
    }
}
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 uses function invocation to add two numbers. 

html




<!DOCTYPE html>
<html lang="en">
<head>
    <title>JavaScript Function Invocation</title>
</head>
 
<body style="text-align:center;">
    <h2 style="color:green">GeeksForGeeks</h2>
    <h4>JavaScript Function Invocation</h4>
    <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>


Output:  

Image

 

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

Javascript




let myObject = {
    firstName: "Geeks",
    middleName: "for",
    lastName: "Geeks",
    fullName: function () {
        return this.firstName + this.middleName
            + this.lastName;
    }
}
console.log(myObject.fullName());


Output

GeeksforGeeks

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

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!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 02 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials