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 How to get the month name from Date using JavaScript ? The purpose of this article is to get the month name from a particular date using JavaScript getMonth() method. This method is used to fetch the month(0 to 11) from the given Date object. It returns an integer value ranging from (0 to 11) Zero (0) means January, 1 means February, and so on, till 11 1 min read How to invoke a function without parenthesis using JavaScript ? Method 1: Using the new operator. The new operator is used to create an instance of an object which has a constructor function. This constructor function can be used to write our own function and then be invoked by the new operator. The parenthesis is optional when using this operator, hence the fun 2 min read How to call a function that return another function in JavaScript ? The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method. Approach:Define Outer Function:Create an outer fun 2 min read How to find out how many Times a Function is Called with JavaScript ? In JavaScript, a function can be called multiple times wherever you need the same kind of functionality. You can also keep track of the function calls to find out how many times it is called in the whole code using the below approaches. Table of Content Using a Counter VariableUsing a Wrapper Functi 2 min read How to Call a JavaScript Function using an onsubmit Event ? HTML has a form element that is used to get the data from the user and send the data to the server. Before the HTML form sends data to the server, it generates an event called "submit". This event can be utilized to execute a JavaScript function that can either validate the form data or notify the u 3 min read Like