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

While you start writing programs for real world problems, you might have notices that there are many actions which need to be perform multiple times. For that you might have write duplicate code at separate places in the code.
That’s where functions come in.
What are functions? Why we need functions?
Functions are a piece of code written that can perform a specific task. There are a code blocks which can be reusable at multiple places.
Instead of writing same logic to separate places you can it inside a function and use it where ever you need it.
Real life analogy
Imagine a coffee machine ☕️
Actions perform in a coffee machine:
Press the button
Grinding
Brewing
Filtering
Gives coffee
These are all the separate methods done by the coffee machine.
Examples: Without function:
const sum1 = 1 + 2; // 3
const sum2 = 2 + 3; // 5
const sum3 = 3 + 4; // 7
const sum4 = 4 + 5; // 9
You can see we have the write the logic to add the number multiple times. Instead of this we can write a function sum which can return the sum of two number. We just had to provide the two numbers to the functions.
Examples: With function:
function sum (number1, number2){
return number1 + number2;
}
const sum1 = sum(1, 2); // 3
const sum2 = sum(2, 3); // 5
const sum2 = sum(3, 4); // 7
const sum2 = sum(4, 5); // 9
How to create functions?
We can create functions by different ways:
Function Declaration Syntax
Function Expression Syntax
Let's discuss them one by one:
Function Declaration Syntax
This is the most common way of creating the functions:
function sum (number1, number2) {
return number1 + number2
}
Key points:
functionkeywordname of the functions (
sum)Can be called anywhere (explained more later in this blog)
Function Expression Syntax
In this approach, a function is stored in a variable:
const sum = function (number1, number2) {
return number1 + number2
}
Key points:
Function is declared using
functionkeyword.Function has no name.
Function reference is stored in a variable. A variable should have a name.
Can be called using the variables. In this case, you can call
sum()as it stores the reference of the function. Ideally, the function is called
Declaration vs Expression (Side-by-Side)
| Feature | Function Declaration | Function Expression |
|---|---|---|
| Syntax | function add() {} |
const add = function() {} |
| Hoisting | ✅ Fully hoisted | ❌ Not fully hoisted |
| Usage before definition | ✅ Works | ❌ Error |
| Common use | General functions | Callbacks, dynamic logic |
Hoisting in Functions
Let's first understand what hoisting is:
Hoisting means: javascript moves declarations to the top before execution.
Example:
console.log(number1); // undefined
var number1 = 10;
You might be wondering that console.log(number1) should give an error instead of printing undefined. In Javascript, this is possible because of the concept of hoisting.
Now try to run the logic below in the browser console dev tool or by creating a JS file and running it using node <filename.js>.
console.log(number2);
// ReferenceError: Cannot access 'number2' before initialization
const number2 = 20;
console.log(number3);
// ReferenceError: Cannot access 'number3' before initialization
const number3 = 30;
Now, you can see the error while executing this code.
Same applicable on the function.
Hoisting works in declaration syntax:
const result = sum(1, 2);
console.log(result); // 3
function sum (number1, number2){
return number1 + number2;
}
Hoisting doesn't work in declaration syntax:
const result = sum(1, 2);
console.log(result);
// ReferenceError: Cannot access 'sum' before initialization
const sum = function (number1, number2){
return number1 + number2;
}
When to Use What?
Use Function Declaration when:
You want simple, reusable functions
You need to call it anywhere in your code
You want a cleaner structure
Use Function Expression when:
You’re passing functions as arguments (callbacks)
You want more control over execution
You’re working with modern patterns (like arrow functions) (Arrow functions we can study in the next blog)
Execution flow of function call
You can also read about Javascript code execution in detail in the blog "Understanding JavaScript Global & Local Execution Context".
For functions, we can understand it stepwise. When a JS code is been executed:
Javascript scans all the code
Hoist function declaration on top
Start execution line by line
When executing the code, if the function called is hoisted, it will execute that function.
Assignment (Try It Yourself)
Write a function declaration that multiplies two numbers and returns the result.
Write the same logic using a function expression approach.
Call both the functions and try to re-run the logic by updating the values passed to the variable.
Call both the functions before they are defined and try to observe the output (error/result).
Conclusion
Functions are the building blocks of any program. You should know that the functions are used for:
Reusability
Combining Logic
Make code clean and readable
Follow my Javacript series to learn more!



