Function Declarations vs Expressions in
JavaScript

Function Declarations vs Expressions in JavaScript

JavaScript functions are one of the most essential building blocks of the language. Understanding how to define and use them correctly is key to writing clean, efficient, and maintainable code. While both function declarations and function expressions define functions, there are key differences that affect how and when they can be used in your code.

In this post, we’ll explore the differences between Function Declarations and Function Expressions, provide code examples, and explain how these differences can impact your code structure and behavior.


What is a Function Declaration?

A Function Declaration is a standard way of defining a function in JavaScript. It consists of the function keyword followed by the function name, a list of parameters, and the function body.

Syntax:

function functionName(parameters) {
    // function body
}

Example:

function greet() {
    console.log("Hello, world!");
}
  • Hoisting: One of the key characteristics of a function declaration is hoisting. Hoisting means that the entire function definition is moved to the top of its scope during the compilation phase. This allows you to call the function before it is defined in your code.

Example with hoisting:

greet(); // Works even before the function is defined

function greet() {
    console.log("Hello, world!");
}

In the example above, the function greet() works even if it's called before the definition.


What is a Function Expression?

A Function Expression is a way to define a function and assign it to a variable. A function expression can be anonymous (without a name) or named.

Syntax:

const functionName = function(parameters) {
    // function body
};

Example:

const greet = function() {
    console.log("Hello, world!");
};
  • No Hoisting: Unlike function declarations, function expressions are not hoisted. This means the function is only available after the expression is evaluated in the code.

Example without hoisting:

greet(); // Error: greet is not a function

const greet = function() {
    console.log("Hello, world!");
};

In this case, the function greet() cannot be called before its definition, because function expressions are evaluated at runtime.


Key Differences Between Function Declarations and Function Expressions

FeatureFunction DeclarationFunction Expression
Syntaxfunction name() { }const name = function() { }
HoistingHoisted and can be called before declarationNot hoisted, must be defined first
NameNamed (must be a valid identifier)Can be anonymous or named
UsabilityAvailable throughout the scopeAvailable only after the expression is evaluated
ScopeFunction available in the whole scopeLimited to the scope where it’s defined

When to Use Function Declarations vs Function Expressions?

  1. Use Function Declarations When:

    • You want the function to be available throughout the entire scope.

    • You prefer clearer syntax and readability.

    • You need the function to be available before it is written in the code (thanks to hoisting).

Example use case:

  • Utility Functions: Functions that are used throughout the codebase can be declared at the top of the file.
  1. Use Function Expressions When:

    • You need to assign a function to a variable, such as when passing a function as an argument to another function (higher-order functions).

    • You want to create anonymous functions or closures.

    • You need the function to only exist in a specific context (like inside a block or after its declaration).

Conclusion

Understanding the differences between function declarations and function expressions is essential for writing clean, effective JavaScript code. While both serve the same purpose of defining a function, they behave differently in terms of hoisting, scope, and usage.

  • Function declarations are great for defining reusable functions that are available throughout the entire scope.

  • Function expressions offer more flexibility, especially when dealing with callbacks, anonymous functions, and closures.

By knowing when and how to use these different function types, you can write more maintainable and efficient JavaScript code.