var functionName = function() {} vs function functionName() {}
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
JavaScript supports both function declarations and function expressions, and they are not interchangeable in all situations. The biggest differences are hoisting behavior, naming, and how each form behaves in modern module code. Understanding those differences helps you avoid runtime surprises and write predictable APIs.
Function Declaration Versus Function Expression
A function declaration defines a named function directly in scope:
A function expression creates a function value and assigns it to a variable:
Both can be called as greet("Ada"), but setup timing is different.
Hoisting Differences
Function declarations are hoisted with their implementation, so they can be called before their textual definition.
With var function expressions, only the variable declaration is hoisted, not the function assignment.
With let or const, the binding is in temporal dead zone before initialization.
This is one reason modern code often prefers explicit ordering over relying on hoisting.
Naming and Debugging Behavior
Function declarations always have a name, which improves stack traces. Function expressions can be anonymous or named.
Named expression example:
The inner name factorialImpl is useful for recursion and debugging, while external callers use factorial.
Scope and Block Behavior
In modern JavaScript, function declarations inside blocks can have engine-specific historical quirks in non-strict contexts. In strict mode and modules, behavior is more predictable, but style consistency still matters.
Function expressions assigned to const inside blocks are explicit and avoid legacy edge cases.
For maintainability, many teams prefer function declarations for top-level reusable helpers and const function expressions for callbacks and closures.
Callbacks and Higher-Order Patterns
Function expressions are common when functions are passed as values.
Arrow functions are a modern shorthand for many expression use cases:
Function declarations are still useful when you want named, hoisted utilities with clear signatures.
Practical Recommendation in Modern Code
A practical style guide that works well:
- Use function declarations for core named utilities in a module.
- Use
constfunction expressions for values passed around as data, especially callbacks. - Avoid
varin new code due to function scope and hoisting confusion.
Example mixed style:
This pattern keeps intent clear and avoids hoisting-related confusion.
Common Pitfalls
A common pitfall is calling a var function expression before assignment and assuming it behaves like a declaration. Another is using var in modern codebases and introducing hard-to-track scope behavior. Teams also overuse anonymous expressions, which can reduce stack trace clarity during debugging. Confusion around block-level function declarations in mixed strict and non-strict environments can create subtle bugs. Finally, inconsistent style across a codebase makes readability and onboarding harder.
Summary
- Function declarations and function expressions differ mainly in hoisting and declaration timing.
- Declarations are hoisted with implementation, while expression assignments happen at runtime.
- Prefer
constfunction expressions overvarin modern JavaScript. - Use declarations for reusable named helpers and expressions for callback-style values.
- Pick a consistent team style to reduce runtime surprises and improve maintainability.

