Thursday, September 4, 2025
HomeLanguagesJavascriptHow to find out the caller function in JavaScript?

How to find out the caller function in JavaScript?

In this article, we see the methods to find out the caller function in Javascript. Sometimes, the developer may want to modify how a function works on the basis of its caller function. To find out the caller function name, we will use the Function object’s caller property.

Property:

Here, the Function object is replaced by the name of the function of which we want to know the parent function name. 

Example 1: When the below code is executed, we can see the name of the parent function being logged out. 

javascript




<script>
    // Child function
    function Foo() {
     
    // This will print 'Bar'
        console.log(Foo.caller.name);
    }
     
    // Parent function
    function Bar() {
    Foo();
    }
     
    Bar();
</script>


Output:

 Bar 

Example 2: Suppose we call the Foo function from multiple functions. 

javascript




<script>
    // Child function
    function Foo() {
     
        // This will print parent function's name
        console.log(Foo.caller.name);
    }
     
    // Parent function
    function Geeks() {
        Foo();
    }
     
    // Parent function
    function Fun() {
        Foo();
    }
     
    // Parent function
    function Sam() {
        Foo();
    }
     
    Geeks();
    Fun();
    Sam();
</script>


Output: 

Geeks
Fun
Sam

 You can know more about the parent function from the property Function.caller

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6628 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS