JavaScript - Function Invocation
JavaScript - Function Invocation
Function Invocation
Thefunction invocationin JavaScript is the process of executing a function. A JavaScript function can be invoked using the function name followed by a pair of parentheses. When you write a function code in JavaScript, it defines the function with expressions and statements. Now, to evaluate these expressions, it is necessary to invoke the function. The function invocation is the synonym of the functioncallor functionexecution.
A JavaScript function can be defined using function declaration or function expression. The code inside the curly braces in the function definition are not executed when function is defined. To execute the code, we need to invoke the function.
The call a function and invoke a function are two interchangeable terms are commonly used. But we can invoke a function without calling it. For example, self-invoking functions are invoked without calling them.
Syntax
The syntax of function invocation in JavaScript is as follows
functionName()
OR
functionName(arg1, arg2, ... argN);Here βfunctionNameβ is the function to be invoked. We can pass as many arguments as the number of parameters listed in function definition.
Example
In the example below, we have defined the merge() function taking two parameters. After that, we used the function name to invoke the function by passing the arguments.
Output
Hello World!Invocation of Function Constructor
When you invoke a function with the βnewβ keyword, it works as a function constructor. The function constructor is used to create an object from the function definition.
Syntax
Following is the syntax to invoke the function as a constructor.
const varName = new funcName(arguments);In the above syntax, we invoked the function with a βnewβ keyword and passed the arguments.
Example
In the example below, we use the function as an object template. Here, the βthisβ keyword represents the function object, and we use it to initialize the variables.
After that, we invoke the function car with the βnewβ keyword to create an object using the function template.
Output
The ODCar object is: {"name":"OD","model":"Q6","year":2020}Object Method Invocation
We havenβt covered JavaScript objects yet in this tutorial but we will cover it in upcoming chapters. Here, letβs learn the object method invocation in short.
The JavaScript object can also contain the function, and it is called the method.
Syntax
Following is the syntax below to invoke the JavaScript object method.
obj.func();In the above syntax, the βobjβ is an object containing the method, and βfuncβ is a method name to execute.
Example
In the example below, we have defined the βobjβ object containing the βnameβ property and the βgetAge()β method.
Outside the object, we access the method by the object reference and invoke the method. In the output, the method prints 10.
Output
The age of John is: 10Self-Invoking Functions
The self-invoking functions in JavaScript are executed just after they are defined. There is no need to call these types of function to invoke them. The self-invoking functions are always defined using anonymous function expression. These types of functions are also called immediately invoked function expressions (IIFEs). To invoke these function, we wrap the function expression within a grouping operator (parentheses) and then add a pair of parentheses.
Example
Try the following example. In this example, we define a function to show a βHello worldβ message in alert box.
Other methods to invoke the function
JavaScript contains two special methods to invoke the function differently. Here, we have explained each method in the table below.
MethodFunction invocationArgumentsCall()Immediate invocationSeparate argumentsApply()Immediate invocationArray of arguments
The difference between the call() and apply() method is how it takes the function arguments. We will learn each of the above methods with examples in the next chapters one by one.