JavaScript function caller Property
Last Updated :
26 May, 2023
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 functions 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.
A few examples are given below for a better understanding of the function methods.
Example 1: This example shows the basic use of the Javascript function.caller property.
Javascript
function func2() {
console.log( "This is from function 2" )
}
function func1() {
func2();
}
func1();
console.log( "from function.caller: " ,
func1.caller)
|
Output:
This is from function 2
from function.caller: null
Example 2: This example shows the basic use of the Javascript function.caller property.
Javascript
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()
|
Output:
The function was called from the top!
We have a complete list of Javascript Function methods, to check those please go through this Javascript Function Complete reference article.
Browsers Supported:
- Google Chrome 1 and above
- Microsoft Edge 12 and above
- Mozilla Firefox 1 and above
- Internet Explorer 8 and above
- Safari 3 and above
- Opera 9.6 and above
Share your thoughts in the comments
Please Login to comment...