Named Functions
Named Functions
Section titled “Named Functions”Introduction
Section titled “Introduction”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.
Example
Section titled “Example”const greet = function sayHello(name) { return `Hello, ${name}!`;};
console.log(greet('Alice')); // 'Hello, Alice!'console.log(sayHello); // ReferenceError — not accessible outsideBenefits
Section titled “Benefits”// Better stack tracesconst factorial = function calc(n) { if (n <= 1) return 1; return n * calc(n - 1); // can use the name recursively};Summary
Section titled “Summary”- Named function expressions:
const fn = function name() {} - Name is only accessible inside
- Better debugging with named stack traces
- Useful for recursion