Explain invoking function in JavaScript
Last Updated :
20 Feb, 2023
In this article, we will learn about invoking the function in Javascript, along with understanding its implementation through examples. Function Invoking is a process to execute the code inside the function when some argument is passed to invoke it. You can invoke a function multiple times by declaring the function only once. When the function is defined, the code inside a function will not be executed. It is common to use the term “call a function” instead of “invoke a function”. Although, there has a difference between these two terms. When you make a function call, you are directly telling the function to execute, whereas when you invoke a function, you are letting something execute the function. For instance,
functionName();
Here, we have invoked the function ie., letting it run, by calling the function directly.
Syntax:
function myFunction( var ) {
return var;
}
myFunction( value );
Here, by calling myFunction, you are invoking value, which is being called indirectly.
Invoking a Function as a Method: We can define the function as an object method.
Syntax:
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.
We will understand the above concepts through examples.
Example 1: This example uses the function invocation to add two numbers.
HTML
<h2 style="color:green">GeeksforGeeks</h2>
<p> Function returns the addition of 10 and 15 </p>
<p id="geeks"></p>
<script>
function add(n1, n2) {
return(n1 + n2);
}
document.getElementById("geeks").innerHTML =
window.add(10, 15);
</script>
Output:
Example 2: This example illustrates this keyword to point to the current object.
JavaScript
const obj={
first_name:"Steve",
last_name:"smith",
name:function(){
console.log(`Full name : ${this.first_name} ${this.last_name}`);
}
};
obj.name();
Output:
Full name : Steve smith
Similar Reads
Explain the MUL() function in JavaScript ? The MUL function is a miniature of the multiplication function. In this function, we call the function that required an argument as a first number, and that function calls another function that required another argument and this step goes on. The first function's argument is x, the second function`s
1 min read
JavaScript Function Invocation In JavaScript, function invocation refers to executing the code defined in a function. Function invocation occurs when a function is called or executed in JavaScript. When invoked, the code inside the function block is run, and any return value is computed and passed back to the caller.JavaScriptfun
3 min read
JavaScript Function Examples A function in JavaScript is a set of statements that perform a specific task. It takes inputs, and performs computation, and produces output. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different
3 min read
JavaScript Function Expression A function expression is a way to define a function as part of an expression making it versatile for assigning to variables, passing as arguments, or invoking immediately.Function expressions can be named or anonymous.They are not hoisted, meaning they are accessible only after their definition.Freq
3 min read
eval() vs. Function() in JavaScript We will learn about JavaScript functions eval() and Function(). The eval() and Function() are used to evaluate any JavaScript expression passed to either of them as a string but the difference between them is how how they handle the expression. eval() The eval() method in JavaScript evaluates or exe
2 min read
JavaScript function* expression The function* is an inbuilt keyword in JavaScript which is used to define a generator function inside an expression. Syntax: function* [name]([param1[, param2[, ..., paramN]]]) { statements}Parameters: This function accepts the following parameter as mentioned above and described below: name: This p
2 min read