Javascript Function.name

The JavaScript Function name property returns the function's name when it was created.

The syntax to access the name property is:

func.name

Here, func is a function.


Return value from Function.name

  • Returns the function's name as specified when it was created. It is anonymous or ''(an empty string) for functions created anonymously.

Example: Using Function.name

function func() {}
console.log(func.name); // func

func.name = "something";
console.log(func.name) // cannot be modified, read-only

const func1 = function () {};
console.log(func1.name); // func1

const object = {
  func2: function () {},
};
console.log(object.func2.name); //func2

// arrow function
console.log((() => {}).name); // ''

console.log((new Function).name); // anonymous

Output

func
func
func1
func2

anonymous

Recommended Reading: JavaScript Function.length