How to Find out the Caller Function in JavaScript
Read this JavaScript tutorial and learn the accepted method of finding out the call stack. Get information about the obsolete method of getting the call.
To find out the caller function, use the non-standard function.caller property in JavaScript. Replace function with the name of the function whose caller you want to identify.
JavaScript caller function
If the function <kbd class="highlighted">Welcome</kbd> was invoked by the top-level code, the value of <kbd class="highlighted">Welcome.caller</kbd> is null; otherwise, it is the function that called <kbd class="highlighted">Welcome</kbd>.
function.caller is a non-standard feature and is not part of any web standard. Avoid using it in production websites, as it may not work consistently across all browsers. Additionally, it only returns the immediate caller, not the full call stack. For modern debugging, use Error().stack or console.trace() instead.
There is another method which is no longer supported in modern JavaScript:
JavaScript caller function
JavaScript caller function
The <kbd class="highlighted">function.caller</kbd> property was introduced to replace the obsolete <kbd class="highlighted">arguments.caller</kbd> property. arguments.caller used to provide the function that called the currently executing function, but it has been removed from modern JavaScript.
Though it may still work in some major browsers, its use is not recommended, as it could be removed at any time. Note that in strict mode or ES modules, function.caller returns null, and arguments.callee throws a TypeError.
The function.caller Property
The <kbd class="highlighted">function.caller</kbd> property returns the function that called the specified function. It will return null for strict, generator function callers and async function.