JavaScript: Function name
1 min read

JavaScript: Function name

Function in JavaScript has a name property.

For lambda and anonymous functions, it is an empty string.

(() => 42).name // prints ''
(function(){return 42;}).name // prints ''

When assigned to variables, the name property takes the value from the variable name.

let theAnswer = () => 42;
theAnswer.name // prints 'theAnswer'

Named function automatically creates the variable.

function getAnswer(){return 42;}
getAnswer.name // prints 'getAnswer'