Open In App

How to get currently running function name using JavaScript ?

Last Updated : 17 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.




    <div id="highlighter_645581" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="plain"><!DOCTYPE html> </code></div><div class="line number2 index1 alt1"><code class="plain"><html>  </code></div><div class="line number3 index2 alt2"><code class="undefined spaces"> </code> </div><div class="line number4 index3 alt1"><code class="plain"><head> </code></div><div class="line number5 index4 alt2"><code class="undefined spaces">    </code><code class="plain"><title>  </code></div><div class="line number6 index5 alt1"><code class="undefined spaces">        </code><code class="plain">How to get currently running </code></div><div class="line number7 index6 alt2"><code class="undefined spaces">        </code><code class="keyword">function</code> <code class="plain">name using JavaScript ? </code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="plain"></title>  </code></div><div class="line number9 index8 alt2"><code class="plain"></head>  </code></div><div class="line number10 index9 alt1"><code class="undefined spaces">     </code> </div><div class="line number11 index10 alt2"><code class="plain"><body style = </code><code class="string">"text-align:center;"</code><code class="plain">>  </code></div><div class="line number12 index11 alt1"><code class="undefined spaces">             </code> </div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="plain"><h1 style = </code><code class="string">"color:green;"</code> <code class="plain">>  </code></div><div class="line number14 index13 alt1"><code class="undefined spaces">        </code><code class="plain">GeeksForGeeks  </code></div><div class="line number15 index14 alt2"><code class="undefined spaces">    </code><code class="plain"></h1>  </code></div><div class="line number16 index15 alt1"><code class="undefined spaces">         </code> </div><div class="line number17 index16 alt2"><code class="undefined spaces">    </code><code class="plain"><script>  </code></div><div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="keyword">function</code> <code class="plain">getFuncName() { </code></div><div class="line number19 index18 alt2"><code class="undefined spaces">            </code><code class="keyword">return</code> <code class="plain">getFuncName.caller.name </code></div><div class="line number20 index19 alt1"><code class="undefined spaces">        </code><code class="plain">} </code></div><div class="line number21 index20 alt2"><code class="undefined spaces">         </code> </div><div class="line number22 index21 alt1"><code class="undefined spaces">        </code><code class="keyword">function</code> <code class="plain">teddy() {  </code></div><div class="line number23 index22 alt2"><code class="undefined spaces">            </code><code class="plain">console.log(getFuncName()) </code></div><div class="line number24 index23 alt1"><code class="undefined spaces">        </code><code class="plain">} </code></div><div class="line number25 index24 alt2"><code class="undefined spaces">        </code><code class="plain">teddy()      </code></div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="plain"></script>  </code></div><div class="line number27 index26 alt2"><code class="plain"></body>  </code></div><div class="line number28 index27 alt1"><code class="undefined spaces"> </code> </div><div class="line number29 index28 alt2"><code class="plain"></html> </code></div></div></td></tr></tbody></table></div>

    
    

    Output:

  • 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 running
            function name using JavaScript ?
        </title> 
    </head> 
          
    <body style = "text-align:center;"
                  
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1> 
              
        <script> 
            function getFuncName() {
                return getFuncName.caller.name
            }
              
            function teddy() { 
                console.log(getFuncName())
            }
            teddy()     
        </script> 
    </body> 
      
    </html>

    
    

    Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads