How to get the function name from within that function using JavaScript ? Last Updated : 25 Aug, 2023 Comments Improve Suggest changes Like Article Like Report Given a function and the task is to get the name of the function from inside the function using JavaScript. There are basically two methods to get the function name from within that function in JavaScript. These are: Using String substr() MethodUsing Function prototype name propertyGet the Function name using String substr() MethodThis 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.Example: This example first converts the function to string using toString() method and then extracts the name from that string using substr() method. JavaScript function functionName(fun) { let val = fun.toString(); val = val.substr('function '.length); val = val.substr(0, val.indexOf('(')); console.log(val); } function GFGFunction() {} functionName(GFGFunction); OutputGFGFunctionGet the Function name using Function prototype name propertyThis 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.nameReturn value: It returns the name of the function. Example: This example gets the name of the function by using function proptotype name property. JavaScript function functionName(fun) { let val = fun.name; console.log(val); } function GFGFunction() { } functionName(GFGFunction); OutputGFGFunction Comment More infoAdvertise with us Next Article How to get the function name from within that function using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to get currently running function name using JavaScript ? 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 2 min read How to Check a Function is a Generator Function or not using JavaScript ? Given an HTML document containing some JavaScript function and the task is to check whether the given function is generator function or not with the help of JavaScript. There are two examples to solve this problem which are discussed below: Example 1:In this example, we will use functionName.constru 2 min read How to get the file name from page URL using JavaScript ? JavaScript provides multiple techniques for string manipulation and pattern matching. By demonstrating various methods, the article equips developers with versatile solutions to dynamically retrieve and utilize file names from different URL formats within their applications. There are several approa 3 min read How to call function from it name stored in a string using JavaScript ? In this article, we will call a function from the string stored in a variable. There are two methods to call a function from a string stored in a variable. Using window object methodUsing eval() method Note: The eval() method is older and is deprecated. Method 1: Using the window object. The window 2 min read How to create a function that invokes each provided function with the arguments it receives using JavaScript ? In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript. It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each prov 3 min read Like