Facebook Pixel

Named Function Expressions and Recursion in JavaScript

Named function expressions give internal names for recursion and better stack traces. Here is how.

Named Function Expressions and Recursion in JavaScript

A named function expression is a function expression with an internal name. The name is only visible inside the function, which is useful for recursion and produces better stack traces.

Basic Named Function Expression

const greet = function sayHi(name) { return `Hello, ${name}`; }; greet("Kunal"); // "Hello, Kunal" sayHi("Kunal"); // ReferenceError: sayHi is not defined (not visible outside) `` The internal name `sayHi` is only accessible inside the function body. Outside, you must use the variable name `greet`. ### Recursion With a Named Function Expression ```js const factorial = function compute(n) { if (n <= 1) return 1; return n * compute(n - 1); // use the internal name for recursion }; factorial(5); // 120 `` `compute` is available inside the function, so it can call itself. This is more robust than using the outer variable: ```js // less robust: if factorial is reassigned, recursion breaks const factorial = function (n) { if (n <= 1) return 1; return n * factorial(n - 1); // uses the outer variable }; `` If someone reassigns `factorial` to something else, the inner `factorial(n - 1)` still refers to the original (because of closure), but it is fragile. ### Better Stack Traces ```js // anonymous: stack trace shows <anonymous> const foo = function () { throw new Error("oops"); }; // named: stack trace shows sayHi const foo = function sayHi() { throw new Error("oops"); }; `` Named function expressions produce clearer stack traces, which makes debugging easier. ### IIFE With a Name ```js const counter = (function count() { let c = 0; return function () { return ++c; }; })(); `` The IIFE has an internal name `count`, useful if it needs to recurse. ### Arrow Functions Cannot Have a Name Arrow functions are always anonymous. For recursion with arrows, use the outer variable: ```js const factorial = (n) => (n <= 1 ? 1 : n * factorial(n - 1)); `` This works but is fragile if `factorial` is reassigned. ### The Takeaway Named function expressions give an internal name that is only visible inside the function. This is useful for recursion (more robust than using the outer variable) and produces better stack traces. Arrow functions are always anonymous, so they must use the outer variable for recursion.

A function expression with an internal name, like const greet = function sayHi() {}. The internal name (sayHi) is only visible inside the function, useful for recursion and better stack traces. Outside, you use the variable name (greet).

Because the internal name is available inside the function for self-reference. This is more robust than using the outer variable, which could be reassigned. The internal name is stable and always refers to the function itself.

Yes. The internal name appears in stack traces instead of <anonymous>, which makes debugging easier. This is one reason to prefer named function expressions for critical code paths.

No. Arrow functions are always anonymous. For recursion with arrows, you must use the outer variable name (const factorial = (n) => n * factorial(n - 1)), which is fragile if the variable is reassigned.

No. The internal name is only visible inside the function body. Outside, you must use the variable name. Trying to access the internal name from outside throws ReferenceError.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.