JavaScript Function Invocation
Last Updated :
17 Dec, 2024
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.
JavaScript
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Meeta"));
- The function greet is invoked with the argument “Meeta”.
- The string “Hello, Meeta!” is returned and logged to the console.
Types of Function Invocation
JavaScript provides several ways to invoke functions. Each method affects the behavior of this (the execution context) and other factors.
1. Function Invocation
When a function is called directly using its name, it operates in the global or local scope.
JavaScript
function add(a, b) {
return a + b;
}
console.log(add(5, 3));
Scope: In non-strict mode, this defaults to the global object (window in browsers).
2. Method Invocation
When a function is a property of an object and is invoked as object.method(), it is called a method invocation.
JavaScript
const user = {
name: "Meeta",
greet: function () {
return `Hello, ${this.name}!`;
},
};
console.log(user.greet());
In method invocation, this refers to the object that owns the method (in this case, user).
3. Constructor Invocation
Functions can be invoked as constructors using the new keyword. When invoked this way, the function creates a new object and sets this to refer to that object.
JavaScript
function Person(name, age) {
this.name = name;
this.age = age;
}
const meeta = new Person("Meeta", 25);
console.log(meeta.name);
A constructor invocation returns the newly created object.
4. Indirect Invocation
Functions can be invoked indirectly using call(), apply(), or bind().
call(): Invokes a function and explicitly sets this and individual arguments.
JavaScript
function greet(greeting) {
return `${greeting}, ${this.name}!`;
}
const user = { name: "Meeta" };
console.log(greet.call(user, "Hello"));
apply(): Similar to call(), but arguments are passed as an array.
JavaScript
function greet(greeting) {
return `${greeting}, ${this.name}!`;
}
const user = { name: "Meeta" };
console.log(greet.apply(user, ["Hi"]));
bind(): Creates a new function with this permanently set to the provided value.
JavaScript
function greet(greeting) {
return `${greeting}, ${this.name}!`;
}
const user = { name: "Meeta" };
const boundGreet = greet.bind(user);
console.log(boundGreet("Hey"));
5. Self-Invoking Functions
Self-invoking (or immediately invoked) functions run automatically without being explicitly called. These are often written as Immediately Invoked Function Expressions (IIFEs).
JavaScript
(function () {
console.log("This is a self-invoking function!");
})();
OutputThis is a self-invoking function!
IIFEs are commonly used for encapsulating code to avoid polluting the global scope.
6. Arrow Function Invocation
Arrow functions are invoked like regular functions but differ in how they handle this. They do not bind their own this; instead, they inherit this from their lexical scope.
JavaScript
const user = {
name: "Meeta",
greet: () => {
return `Hello, ${this.name}!`;
// `this` here is not bound to `user`
},
};
console.log(user.greet());
Similar Reads
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. [GFGTABS] Ja
3 min read
Functions in JavaScript
Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs. [GFGTABS] JavaScript function sum(x, y) { return x + y; } console.log(sum(6, 9)); [/GFGTABS]Output1
5 min read
JavaScript Function Generator
A generator function is a special type of function that can pause its execution at any point and resume later. They are defined using the function* syntax and use the yield keyword to pause execution and return a value. Syntax function* generatorFunction() { // Code that can yield multiple values }[
3 min read
JavaScript Function Definitions
JavaScript functions are declared using the function keyword, either as a declaration or expression. Declarations define named functions, while expressions assign functions to variables. Both enable code reuse and modularity. Syntax Function Declarations function functionName( parameters ) { // Stat
3 min read
Explain invoking function in JavaScript
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 declar
2 min read
JavaScript Function Binding
In JavaScript, function binding refers to the process of associating a function with a specific context (this value). The bind() method creates a new function that, when called, has its 'this' keyword set to the provided value. this Binding: Functions in JavaScript are executed in a context. By defa
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() Constructor
The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
2 min read
JavaScript Function Call
The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). This allows borrowing methods from other objects, executing them within a different context, overriding the default value, and passing arguments. Syntax: cal
2 min read
JavaScript Anonymous Functions
Functions are first-class citizens, meaning they can be treated as values and passed around like any other variable. One of the key concepts related to this is anonymous functions. What are Anonymous Functions?An anonymous function is simply a function that does not have a name. Unlike named functio
3 min read