w3resource

JavaScript: Get the function name

JavaScript Function: Exercise-29 with Solution

Write a JavaScript function to get the function name.

Sample Solution-1:

JavaScript Code:

// Define a function named abc
function abc() {
    // Log the name of the current function to the console using arguments.callee.name
    console.log(arguments.callee.name);
}

// Call the abc function
abc(); 

Output:

abc

Flowchart:

Flowchart: JavaScript function: Get the function name' as parameter

Live Demo:

See the Pen JavaScript - Get the function name-function-ex- 29 by w3resource (@w3resource) on CodePen.


Sample Solution-2:

JavaScript Code:

// Define an arrow function
const myFunction = () => {
    // Log the name of the function to the console
    console.log(myFunction.name);
};
// Call the arrow function
myFunction();

Output:

myFunction

Flowchart:

Flowchart: JavaScript function: Get the function name' as parameter

Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript program to pass a 'JavaScript function' as parameter.
Next: Javascript Recursion Functions Exercises

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript-exercises/javascript-function-exercise-29.php