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

🧑💻 Software Developer @ TransUnion ⏳ 4+ Years Experience 🧠 Unsorted Array 🚀 Scalable Web Apps 🎥 Tech Educator in Progress
Search for a command to run...

🧑💻 Software Developer @ TransUnion ⏳ 4+ Years Experience 🧠 Unsorted Array 🚀 Scalable Web Apps 🎥 Tech Educator in Progress
No comments yet. Be the first to comment.
Why Do Developers Use Object-Oriented Programming? When you start writing small JavaScript programs, you often store data in variables or simple objects. But as your applications grow, you begin to no

What Objects Are and Why They Are Needed? In JavaScript, objects are used to store related information together in a structured way. If you think about real-world things, most of them have multiple pi

What Arrays Are and Why We Need Them? When writing programs, we often need to store multiple related values. Imagine keeping a list of your favorite movies, a list of marks, or a list of daily tasks.

What Variables Are and Why They Are Needed? When writing a program, we often need a place to store information. This information could be a person’s name, age, score, or whether someone is logged in.

When writing JavaScript, you’ll often repeat the same logic multiple times. For example, imagine you frequently need to add two numbers.
Without functions, you might write something like this again and again:
let result = 5 + 3;
console.log(result);
Now imagine you need to do this 20 times in different parts of your program.
Instead of repeating the logic, we create a function.
A function is simply a reusable block of code that performs a specific task.
Think of a function like a machine: you give it some input, it performs a task, and it returns an output.
Example:
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // 8
Here, the function add takes two numbers and returns their sum. Instead of rewriting the logic, we just call the function whenever needed.
A function declaration is the most common way beginners learn to create functions.
function functionName(parameters) {
// code to execute
}
Example: multiplying two numbers.
function multiply(a, b) {
return a * b;
}
console.log(multiply(4, 5)); // 20
Here’s what’s happening:
function is the keyword used to define a function.
multiply is the name of the function.
a and b are parameters.
The function returns the result of a * b.
Once declared, this function can be used anywhere in your program.
A function expression is another way to create functions. Instead of declaring a function directly, we store the function inside a variable.
let variableName = function(parameters) {
// code
};
Example using the same multiplication logic:
let multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 5)); // 20
The result is the same, but the way the function is defined is slightly different.
Here:
A function is created.
That function is assigned to the variable multiply.
This means the variable now holds the function.
Let’s compare both approaches.
function multiply(a, b) {
return a * b;
}
let multiply = function(a, b) {
return a * b;
};
Both functions work the same way when called:
console.log(multiply(3, 4)); // 12
But there are some important differences between them.
The biggest difference comes from how JavaScript loads and executes code.
Function declarations are hoisted, but function expressions are not.
Hoisting simply means JavaScript moves certain declarations to the top of the file before execution.
Let’s see what this means in practice.
console.log(multiply(3, 4));
function multiply(a, b) {
return a * b;
}
This works perfectly.
Output:
12
Even though the function is written later in the code, JavaScript already knows about it because function declarations are hoisted.
console.log(multiply(3, 4));
let multiply = function(a, b) {
return a * b;
};
This will cause an error.
Why?
Because the variable multiply does not contain the function yet when JavaScript tries to run the first line.
In simple terms:
Function declaration → available before definition
Function expression → available only after assignment
Both approaches are useful, and developers use them for different reasons.
You want functions available anywhere in the file
The function represents a core piece of logic
You want simpler, cleaner syntax
Example:
function calculateTotal(price, tax) {
return price + tax;
}
You want to store functions inside variables
You want functions to behave like values
You plan to pass the function as an argument later
Example:
let greet = function(name) {
return "Hello " + name;
};
Function expressions are also common in callbacks and modern JavaScript patterns.