Given a function and the task is to get the name of function that is currently running using JavaScript.
- Approach 1: Using arguments.callee method: It refers to the currently executing function inside the function body of that function. In this method, we use arguments.callee to refer to the name of the function. So we define a new variable as arguments.callee.toString(). Then we use (variable_name).substr to get the function and then we display it as an alert.
Syntax:
arguments.callee.toString()
You may have to parse the name though, as it will probably include some extra junk.
Example 1: This example display currently running function using arguments.callee method.
<!DOCTYPE HTML><html><head><title>How to get currently runningfunctionname using JavaScript ?</title></head><body style ="text-align:center;"><h1 style ="color:green;">GeeksForGeeks</h1><script>functionGFGs() {varme = arguments.callee.toString();me = me.substr('function '.length);me = me.substr(0, me.indexOf('('));alert(me);}GFGs();</script></body></html>chevron_rightfilter_noneOutput:
- Approach 2: With the help of console.log method, we define a new function which returns the name of the function and call it the present function.
Syntax:
function getFuncName() { return getFuncName.caller.name } function teddy() { console.log(getFuncName()) } teddy()Example 2: This example display currently running function using console.log method.
<!DOCTYPE html><html><head><title>How to get currently runningfunctionname using JavaScript ?</title></head><body style ="text-align:center;"><h1 style ="color:green;">GeeksForGeeks</h1><script>functiongetFuncName() {returngetFuncName.caller.name}functionteddy() {console.log(getFuncName())}teddy()</script></body></html>chevron_rightfilter_noneOutput:
Recommended Posts:
- How to get the function name from within that function using JavaScript ?
- How to get the file name from full path using JavaScript ?
- How to get the file name from page URL using JavaScript ?
- How to get the month name from Date using JavaScript ?
- How to get the first key name of a JavaScript object ?
- How to get the function name inside a function in PHP ?
- How to make sure that Apache service keeps running in Ubuntu
- How to check whether a script is running under Node.js or not ?
- How to get file input by selected file name without path using jQuery ?
- How to get city name by using geolocation ?
- How to call function from it name stored in a string using JavaScript?
- How to get current function name in PHP?
- How to get name of calling function/method in PHP ?
- How to extract the host name from URL using JavaScript ?
- How to add a property to a JavaScript object using a variable as the name?
- How to get file name from a path in PHP?
- How to get a variable name as a string in PHP?
- JavaScript Function name Property
- JavaScript SyntaxError - Function statement requires a name
- JavaScript | WebAPI | File | File.name Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

