Course contentsShow
← JavaScript
Lesson 388 of 2,72311. FunctionsFree lesson

When to Use Declarations vs Expressions

Understand practical guidelines for choosing between declarations and expressions.

When to Use Declarations vs Expressions

You'll learn practical guidelines for choosing between function declarations and expressions in real code.

The Decision Framework

Now that you understand how declarations and expressions differ, when should you use each? The choice often comes down to three factors: hoisting needs, code organization, and intent.

Use Function Declarations When:

1. You want natural top-to-bottom reading
Declarations hoist, so you can call helper functions before defining them, keeping your main logic at the top of a file where readers see it first.

2. You're defining top-level, named functions
For standalone functions that form your module's public interface, declarations clearly signal "this is a reusable function" rather than "this is a value."

3. Recursion is clearer
Named declarations reference themselves naturally without extra syntax.

Use Function Expressions When:

1. You're assigning functions conditionally
If which function you use depends on runtime conditions, expressions assigned to variables work better than declarations (which are hoisted unconditionally).

2. You're passing functions as arguments
Callbacks and higher-order function patterns naturally use expressions — they're values being passed around.

3. You want block-scoped functions
Using const or let with expressions gives you tighter control over scope than declarations (which are function-scoped).

4. You want to prevent accidental calls before initialization
Expressions don't hoist, so you can't accidentally call them before they're defined.

The Consistency Principle

Many teams standardize on one style for consistency. Modern codebases often prefer expressions with const for stricter scoping and no-hoist behavior, but declarations remain perfectly valid and common.

Key Takeaway: Use declarations for top-level, named functions you want available throughout a scope; use expressions when treating functions as values, assigning conditionally, or wanting stricter scoping control.