How to get the function name from within that function using JavaScript ?

Given a function and the task is to get the name of the function from inside the function using JavaScript.

  • JavaScript String substr() Method: This method gets parts of a string, starting at the character at the defined position, and returns the specified number of characters.

    Syntax:



    string.substr(start, length)

    Parameters:

    • start: This parameter is required. It specifies the position from where to begin the extraction. The first character is at index 0. If start is positive and greater than or equal to the length of the string, this method returns an empty string. If start is negative, this method uses it as an index from the end. If start is negative or larger than the length of the string, start is used as 0.
    • length: This parameter is optional. It specifies the number of characters to extract. If not used, it extracts the whole string.

    Return value: It returns a new string, having the extracted part of the text. If length is 0 or negative, an empty string is returned.

  • Function proptotype name property: This is a Function object’s read-only name property denoting the function’s name as defined when it was designed, or “anonymous” when created anonymously.

    Syntax:

    func.name

    Return value: It returns the name of the function.

Example 1: This example first converts the function to string using toString() method and then extracts the name from that string using substr() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to get the function name
            from within that function
        </title>
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_click()">
            Get Name
        </button>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            el_up.innerHTML = GFGFunction;
              
            function functionName(fun) {
                var val = fun.toString();
                val = val.substr('function '.length);
                val = val.substr(0, val.indexOf('('));
                el_down.innerHTML = val;
            }
            function GFGFunction() {
              
            }
            function GFG_click() {
                functionName(GFGFunction);
            }         
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example gets the name of the function by using function proptotype name property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to get the function name
            from within that function
        </title>
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_click()">
            Get Name
        </button>
          
        <p id = "GFG_DOWN" style
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            el_up.innerHTML = GFGFunction;
              
            function functionName(fun) {
                var val = fun.name;
                el_down.innerHTML = val;
            }
            function GFGFunction() {
              
            }
            function GFG_click() {
                functionName(GFGFunction);
            }         
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.