Skip to content

Named Functions

Named function expressions give a name to a function expression. The name is only accessible inside the function body but appears in stack traces for better debugging.

const greet = function sayHello(name) {
return `Hello, ${name}!`;
};
console.log(greet('Alice')); // 'Hello, Alice!'
console.log(sayHello); // ReferenceError — not accessible outside
// Better stack traces
const factorial = function calc(n) {
if (n <= 1) return 1;
return n * calc(n - 1); // can use the name recursively
};
  • Named function expressions: const fn = function name() {}
  • Name is only accessible inside
  • Better debugging with named stack traces
  • Useful for recursion