Skip to content
Related Articles

Related Articles

Improve Article

JavaScript function.caller Property

  • Last Updated : 14 Jul, 2020

The function.caller property of the JavaScript function object returns the function that invoked the specified function. It returns null if the function “f” was invoked by the top-level code in JavaScript. For functions like strict functions, async functions and generator function this method returns null.

Syntax: 

function.caller

Parameters: This function does not accept any parameter.

Return Value: This function returns null for strict, async function and generator function callers.

Few examples are given below for a better understanding of the function methods.



Example 1:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, 
        initial-scale=1.0">
</head>
  
<body>
    <script>
  
        // Creating func2
        function func2() {
            console.log("This is from function 2")
        }
        // Creating func1
        function func1() {
            func2();
        }
  
        // Call Function 1
        func1();
  
        // null is returned as function
        // is strict, async function and 
        // generator function
        console.log("from function.caller: ", 
                                func1.caller)
    </script>
</body>
  
</html>

Output:

Example 2:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
            "width=device-width, 
            initial-scale=1.0">
</head>
  
<body>
    <script>
        function myFunc() {
            if (myFunc.caller == null)
                console.log("The function "
                    + "was called from the top!");
            else
                console.log("This function's "
                    + "caller was " + myFunc.caller);
    }
        myFunc()
    </script>
</body>
  
</html>

Output:

Browsers Supported:

  • Google Chrome
  • Microsoft Edge
  • Mozilla Firefox
  • Internet Explorer
  • Safari
  • Opera

Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :