GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Introduction3 min read
  • Variables5 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Function Invocation - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Function Invocation

Last Updated : 11 Jul, 2025

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")); 

Output
Hello, 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)); 

Output
8

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()); 

Output
Hello, Meeta!

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); 

Output
Meeta

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"));

Output
Hello, Meeta!

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")); 

Output
Hey, Meeta!

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!");
})();

Output
This 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());

Output
Hello, undefined!
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.