Get current function name in JavaScript
This post will discuss how to get the name of the current function being executed in JavaScript.
A function is a block of code that can be defined, invoked, and reused in a program. Getting the name of the current function means obtaining a string that represents the identifier or the expression that defines the function. Here are some of the methods that we can use to get the name of the current function being executed in JavaScript.
1. Using arguments.callee.name property
One way is to use the arguments.callee property, which is a reference to the current function being executed. We can use the name property of arguments.callee to get the name of the function as a string, or use arguments.callee.toString() and parse out the name from the function definition.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function foo() { console.log(arguments.callee.name); // foo } let bar = function() { console.log(arguments.callee.name); // bar }; let baz = function qux() { console.log(arguments.callee.name); // qux }; let foobar = () => { console.log(arguments.callee.name); }; foo(); bar(); baz(); foobar(); |
Output:
foo
bar
qux
Uncaught ReferenceError: arguments is not defined
However, this function is deprecated and forbidden in strict mode, as it can cause security and performance issues. It may also not work for anonymous functions or arrow functions.
2. Using Function.prototype.name property
The recommended option is to use the name property of the Function object, which returns the name of the function as a string. This property is more compatible with strict mode and works for named functions, either declared or assigned to a variable, but not for anonymous functions or arrow functions, as they do not have a name property.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function foo() { console.log(foo.name); // foo } let bar = function() { console.log(bar.name); // bar }; let baz = function qux() { console.log(baz.name); // qux console.log(qux.name); // qux }; let foobar = () => { console.log(foobar.name); // foobar }; let quux = foo.bind(null); console.log(quux.name); // bound foo let corge = new Function("console.log('corge')"); console.log(corge.name); // anonymous foo(); bar(); baz(); foobar(); quux(); corge(); |
Output:
bound foo
anonymous
foo
bar
qux
qux
foobar
foo
corge
3. Using Error.prototype.stack property
The Error.prototype.stack property returns the string representing the stack trace of an error object. This property can be used to get the name of the current function by creating an error object and parsing its stack property. However, it is not a standard property and may not be supported or consistent across different browsers or environments. Also, it may contain some extra information that needs to be filtered out to get the function name.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
function extractFunctionNameFromStack(stack) { // Split the stack into lines and get the second line // (the first line is the error message) let line = stack.split("\n")[1]; // Extract the function name from the line // (assuming it has the format "at FunctionName (file:line:column)") let match = line.match(/at (\S+) \(/); // Return the function name or "anonymous" if not found return match ? match[1] : "anonymous"; } function foo() { // Create an error object and get its stack property let stack = new Error().stack; console.log(extractFunctionNameFromStack(stack)); // foo } let bar = function() { let stack = new Error().stack; console.log(extractFunctionNameFromStack(stack)); // bar }; let baz = function qux() { let stack = new Error().stack; console.log(extractFunctionNameFromStack(stack)); // qux }; let foobar = () => { let stack = new Error().stack; console.log(extractFunctionNameFromStack(stack)); // foobar }; foo(); bar(); baz(); foobar(); |
Output:
foo
bar
qux
foobar
That’s all about getting the name of the current function being executed in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)