foundation

Function declarations and expressions

Declaration hoisting, expression assignment timing, named functions, and stack traces.

Function declarations create a hoisted binding callable before their textual position. Function expressions are values assigned at runtime — anonymous or named. Named function expressions improve stack traces and enable recursion without a global name.

`const fn = function named() {}` is not callable before its line because `const` follows TDZ rules.

Declarations inside blocks behave inconsistently in older environments; prefer `let`/`const` expressions in block scope for portability.

On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.

Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.

The trade-off is often clarity versus performance or safety versus convenience.

Checklist:

  • Identify declaration versus expression.
  • Explain hoisting differences.
  • Prefer named expressions for debugging.