Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

Where do a function's name and length come from?

Questions in this chapter (9)
  • Draft
  • medium
  • Askedsometimes
  • Verified October 2026

Answer

Every function gets two own properties at creation, through SetFunctionName and SetFunctionLength. Both are { writable: false, enumerable: false, configurable: true }, so assignment fails (a TypeError in strict code) but Object.defineProperty can replace them.

name. A declaration or named expression uses its own identifier. An anonymous function or class gets a name only when the spec runs NamedEvaluation on it, which happens when IsAnonymousFunctionDefinition is true (a function or class with no own name, parentheses allowed) in these positions:

  • an initializer of a plain identifier: const f = () => {}, let g = function () {}, a parameter or destructuring default;
  • assignment to a plain identifier, including ||=, &&=, ??=;
  • an object-literal property or class field: the property key, so { run: () => {} } gives 'run';
  • export default function () {}, which gives 'default'.

Assigning to a member — obj.run = function () {} — is not one of them, so name is ''. Other forms add a prefix or wrapper: getters and setters get 'get x'/'set x', Symbol keys become '[description]', private methods '#x', bound functions 'bound f', and new Function gives 'anonymous'.

length. For ordinary functions it's ExpectedArgumentCount: the number of formal parameters to the left of the first one with a default or the rest parameter. A destructuring pattern counts as one parameter. Built-ins have a length fixed by their spec entry: Math.max.length is 2, Array.prototype.push.length is 1.

Neither property changes behaviour — you can call any function with any number of arguments. Code reads them for debugging and stack traces (name) and for arity-based helpers like auto-currying (length). Both are fragile there: minifiers may rename functions, and adding a default parameter silently lowers length.

Example

const arrow = () => {};
const wrapped = (() => {});    // parentheses still count as anonymous
const lost = (0, () => {});    // a comma expression isn't a function definition
const obj = { run() {}, get size() { return 1; }, [Symbol('tag')]() {} };
obj.later = function () {};    // member assignment: no NamedEvaluation

console.log(arrow.name, wrapped.name, JSON.stringify(lost.name)); // → arrow wrapped ""
console.log(obj.run.name, Object.getOwnPropertyDescriptor(obj, 'size').get.name); // → run get size
console.log(obj[Object.getOwnPropertySymbols(obj)[0]].name); // → [tag]
console.log(JSON.stringify(obj.later.name), arrow.bind(null).name); // → "" bound arrow

const lengths = [
  function (a, b, c) {},
  function (a, b = 1, c) {},   // stops at the first default
  (a, ...rest) => {},          // rest never counts
  ({ x, y }, [z]) => {},       // each pattern is one parameter
];
console.log(lengths.map((f) => f.length)); // → [ 3, 1, 1, 2 ]
console.log(Math.max.length, parseInt.length); // → 2 2

const f = function () {};
try { f.name = 'renamed'; } catch (e) { console.log(e.name); } // → TypeError
Object.defineProperty(f, 'name', { value: 'renamed' });
console.log(f.name); // → renamed

Pitfalls

  • Expecting obj.method = function () {} to be named 'method'. Only identifier targets, initializers and literal keys trigger name inference.
  • Using fn.length to decide how many arguments to pass. Defaults, rest parameters and arguments-based functions all report fewer than they accept.
  • Branching on fn.name or constructor.name in production code. Minified builds may rename it, and a class can override it with a static name member.
  • Trying to set name with assignment. It's non-writable; use Object.defineProperty.

Follow-ups

Why does the stack trace show api.load when api.load.name is ''?
Stack traces are a host and engine feature, not name. V8's parser runs its own name inference over assignments, declarations and object literals for anonymous functions, and uses that inferred name in traces. The spec-visible name stays ''.
What do bound functions report?
'bound ' plus the target's name, and the target's length minus the bound arguments, never below zero. The details, including non-String names, are in the bind question.
Can a class's name differ from its binding?
Yes. class A { static name = 'X' } defines a static field after SetFunctionName has run, so A.name is 'X'. A named class expression also keeps its own name: const B = class Inner {} gives B.name === 'Inner'.

Exercises

Predict the output

let handler;
handler ??= () => {};
const { onError = function () {} } = {};
const api = {};
api.onLoad = () => {};
console.log(handler.name, onError.name, JSON.stringify(api.onLoad.name));

Predict the output

const fns = [(a, b) => {}, (a = 0, b) => {}, (...args) => {}, function (a, [b, c], d = 1) {}];
console.log(fns.map((f) => f.length));

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.