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

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
In this article we'll learn about function declaration, function expression and the difference between them. Before jumping on the topic itself let's understand the function meaning and it's use. Functions are nothing but reusable block of code. We make them to perform repeated work efficiently. It help us follow the important rule of 'DRY' (DO NOT REAPEAT YOURSELF).
If you want to add two numbers, you will probably write it like this:
let sum1 = 5 + 3;
console.log(sum1);
let sum2 = 10 + 7;
console.log(sum2);
But if you have to get multiple sum of 2 numbers then the code become redundant and messy which becomes hard to manage.
That is where we write function:
function add(a, b) {
return a + b;
}
console.log(add(5, 3));
console.log(add(10, 7));
Now we can reuse the same function whenever we need to add two numbers.
One common way to create a function is using a function declaration.
Example:
function greet() {
console.log("Hello, welcome to JavaScript!");
}
To run the function, we simply call it:
greet();
Output:
Hello, welcome to JavaScript!
Functions can also accept parameters.
function greet(name) {
console.log("Hello " + name);
}
greet("Sam");
Output:
Hello Sam
Another way to create a function is using a function expression.
Here, the function is stored inside a variable. If you still have doubt about what is a variable then I would recommend you read this blog first to get the hang of things here.
Example:
const greet = function () {
console.log("Hello from function expression");
};
greet();
Output:
Hello from function expression
We can also pass parameters here:
const multiply = function (a, b) {
return a * b;
};
console.log(multiply(4, 5));
Output:
20
Syntax difference
Function declaration is defined directly using the function keyword.
Function expression stores the function inside a variable.
Hoisting
Function declarations are hoisted, so they can be called before they are defined.
Function expressions are not hoisted in the same way, so they must be defined before they are called.
Usage
Function declarations are commonly used for reusable functions in a program.
Function expressions are often used when functions need to be assigned to variables or used as callbacks.
Readability
Function declarations are usually easier to read in larger programs.
Function expressions provide more flexibility in certain situations.
Hoisting is the behaviour where all the function declaration are moved to the top of their scope.
This means we can call a function declaration before it appears in the code.
Example:
sayHello();
function sayHello() {
console.log("Hello!");
}
This works because JavaScript internally treats it like:
function sayHello() {
console.log("Hello!");
}
sayHello();
However, function expressions are not hoisted in the same way, so they must be defined before being used.
Functions are one of the most important concepts in JavaScript. They allow us to group reusable logic into a single block of code and call it whenever needed.
By understanding function declarations, function expressions, and the basic idea of hoisting, we can write cleaner, more organized, and maintainable JavaScript programs.