w3resource

JavaScript: Log the name of a function

JavaScript fundamental (ES6 Syntax): Exercise-228 with Solution

Log Function Name

Write a JavaScript program to log the name of a function.

  • Use console.debug() and the name property of the passed function to log the function's name to the debug channel of the console.
  • Return the given function fn.

Sample Solution:

JavaScript Code:

// Define a function called functionName
const functionName = fn => (console.debug(fn.name), fn);
// Log the name of the provided function and return the function itself
console.log(functionName(Math.max));

Flowchart:

flowchart: Log the name of a function.

Live Demo:

See the Pen javascript-basic-exercise-228-1 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript program that logs the name of a given function by accessing its name property.
  • Write a JavaScript function that wraps another function and logs its name before executing it.
  • Write a JavaScript program that inspects a function’s toString() output to extract its declared name.
  • Write a JavaScript function that verifies if a function is anonymous or named, then logs the appropriate message.

Go to:


PREV : n Random Unique Elements.
NEXT : Camelcase to String.

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.