Skip to main content

Command Palette

Search for a command to run...

Function Declaration vs Function Expression: What’s the Difference?

Updated
3 min readView as Markdown

In JavaScript, there are two primary ways to define functions: function declarations and function expressions. While they both achieve the ultimate goal of creating a reusable block of code, they differ in syntax, behavior, and use cases. Understanding these differences is crucial for writing clean, predictable, and maintainable JavaScript code.

Function Declaration

A function declaration, also known as a function statement, defines a function with a specific name. It is characterized by the use of the function keyword followed by the function name.

function sayHello(name) {

console.log(Hello, ${name}!);

}

Key Characteristics:

Hoisting: Function declarations are hoisted to the top of their scope. This means you can call the function even before it is defined in your code.

Named Functions: Function declarations must have a name. This name is used to identify the function and can be used for recursion (calling the function within itself).

Create Bindings: Function declarations create a binding for the function name in the variable object of the current scope.

Function Expression

A function expression defines a function within an expression. It can be assigned to a variable, passed as an argument to another function, or returned from a function.

const sayHelloExpression = function(name) {

console.log(Hello, ${name}!);

};

Key Characteristics:

Not Hoisted: Unlike function declarations, function expressions are not hoisted. You must define the function expression before you can call it.

Anonymous or Named: Function expressions can be anonymous (without a name) or named. Named function expressions can be useful for debugging and recursion within the function itself.

Assigned to Variables: Function expressions are typically assigned to variables, which makes them flexible for pass-by-reference.

Main Differences:

Hoisting: This is the most significant difference. Function declarations are hoisted, allowing them to be called before their definition. Function expressions are not hoisted, requiring them to be defined before they are called.

Syntax: Function declarations use the function keyword followed by the name, while function expressions are part of an expression (often assigned to a variable).

Named vs. Anonymous: Function declarations must be named. Function expressions can be anonymous or named.

Use Cases:

Function Declarations:

Best suited for defining global functions that need to be accessible throughout your script.

Useful when you want to define functions that can be called before they are defined (e.g., in a main program loop).

Can be used for helper functions that are intended to be reused frequently.

Function Expressions:

Ideal for defining functions within other functions (inner functions).

Commonly used as callbacks for asynchronous operations (e.g., event listeners, setTimeout).

Useful for creating self-contained units of code (immediately invoked function expressions or IIFEs).

Preferred when you want to assign a function to a variable or pass it as an argument.

sayHello("Alice");

function sayHello(name) {

 console.log(Hello, ${name}!);

}
const sayHelloExpression = function(name) {

 console.log(Hello, ${name}!);

};

sayHelloExpression("Bob");

In the example above, sayHello (a function declaration) can be called before its definition because of hoisting. However, sayHelloExpression (a function expression) cannot be called before its definition, and attempting to do so will result in a ReferenceError.

Conclusion

In conclusion, both function declarations and function expressions are fundamental concepts in JavaScript, and understanding their differences is essential for writing effective code. Choose the appropriate method based on your specific needs, considering factors like hoisting, named vs. anonymous functions, and how the function will be used in your application.