Can I get the name of the currently running function in JavaScript?

In this tutorial, we will learn how to get the name of the currently running function in JavaScript?
Submitted by Pratishtha Saxena, on July 24, 2022

Yes!, we can get the name of the currently running function in JavaScript. There is a method for this using which we can easily return the currently running function.

Using callee() Method

callee() is a JavaScript's inbuilt method. If the name of the running function is unknown, we can used .callee() method to get the name of that function. Callee is a property of arguments object. Therefore, we'll use arguments.callee to get the name of the function.

Syntax:

console.log(arguments.callee.name);

.name will return the name of the currently running function. So, when the function is called, therefore, when the function is in running state presently then callee calls that function and name will return it name in a string.

Let's see a basic example for better understanding.

Example:

function One() {
   console.log(arguments.callee.name);
}
One();

function abc() {
    console.log( arguments.callee.name );
}
abc();

Output:

Example: Get the name of the currently running function

JavaScript Examples »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.