Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Function

Function.name

Reference for JavaScript Function.name: how functions get their names, inferred names for variables and methods, bound and getter prefixes, and uses.

The name property of a function is a string holding its name. Declarations take the name written in the source. Anonymous functions and arrow functions get a name inferred from where they are assigned, which makes stack traces and debugging output readable.

The name is read-only information. It is useful for logging, error messages and tooling, but it is not a safe key for program logic.

JavaScript
function greet() {}
greet.name; // "greet"

Try it

JS Declared and inferred names

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Bound functions, classes and symbols

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
fn.name

Value: a string, empty when the function has no name.

The property is not writable and not enumerable, but it is configurable, so Object.defineProperty() can change it.

Usage notes

  • Inference rules. Anonymous function and class expressions get a name from a variable, a property key, a default value or a parameter default. A function passed directly as an argument stays anonymous with an empty name.
  • Explicit names win. In const a = function b() {}, the name is b; the variable a is not used.
  • Prefixes. Getters report "get value", setters "set value", and bound functions "bound original". Symbol-keyed methods use the symbol description in square brackets, such as "[action]".
  • Classes. A class has its class name; static and instance methods have their method names.
  • new Function() produces a function named "anonymous".
  • Minification renames functions, so never depend on name for logic in code that gets bundled.
  • Private methods report names beginning with #.
  • Debugging aid: naming callbacks (array.map(function double(n) {...})) improves stack traces.

Browser support

Function.name is supported in all current browsers. Inference rules for arrow functions and methods follow ES2015 and later.