Function Declaration vs Function Expression: What's the Difference

In JavaScript, there are two main ways to create a function:
Function Declaration
Function Expression
Let’s understand them one by one in simple words.
1. Function Declaration
A function declaration is when you define a function using the function keyword without storing it in a variable.
Example:
function myIntro() { console.log("My name is Renil, I am from ChaiCode Cohort"); }The function has a name (
myIntro).It can be called before the function is written. means you can call anywhere in the code.
Example:
greet(); // working fine, no issue function hobby() { console.log("My hobby is exploring new things"); }
2. Function Expression
A function expression is when you create a function and store it inside a variable.
Example:
const sayBye = function() { console.log("Goodbye! Mummy"); };The function is stored in a variable (
sayBye).The function has no name inside.
You must define the function before using it.
Example:
sayBye(); // ❌ Show error const sayBye = function() { console.log("Goodbye!"); };
When to Use What?
Use function declaration when you need a function that can be called anywhere in your code.
Use function expression when you need a function for a short time, like inside another function.
Hope this makes it super clear! 🙂
About the author
A web developer, certified in Java. Pursuing a BCA in the third year and currently learning full-stack web development from Chaicode.
Comments
Join the discussionNo comments yet. Be the first to comment.






