Functions are where JavaScript actually does something.
Variables store data.
Conditionals decide.
Functions make things happen.
And trust me — understanding how functions are defined changes how you write clean, bug-free code.
🧠 What Is a Function (In Real Projects)?
A function is a reusable block of code that performs a task.
function greet() {
return "Hello!";
}
Instead of repeating logic everywhere, you write it once and call it when needed.
In real apps, functions handle:
- UI actions
- API calls
- Data processing
- Validation logic
📢 Function Declaration (The Classic One)
This is the traditional way to define a function.
function add(a, b) {
return a + b;
}Why developers still use it
- Clean and readable
- Can be called before it’s defined (hoisting)
add(2, 3); // ✅ works
function add(a, b) {
return a + b;
}
This behavior can be helpful — but also confusing if you don’t know it exists.
📦 Function Expression (More Controlled)
A function expression assigns a function to a variable.
const multiply = function (a, b) {
return a * b;
};
What changes?
- Not hoisted like declarations
- Must be defined before use
multiply(2, 3); // ❌ error
This gives you more predictable execution flow.
⚡ Arrow Functions (Modern & Compact)
Arrow functions are the modern favorite.
const subtract = (a, b) => a - b;
They’re shorter and easier to read — especially for small functions.
🧠 Arrow Functions Behave Differently (Important!)
Arrow functions
do not have their own
this.
const user = {
name: "Sam",
greet: () => {
console.log(this.name);
}
};
This logs undefined.
In real projects, I avoid arrow functions for object methods unless I’m very sure about context.
⚖️ When to Use Which (Real-World Guidance)
- Function Declaration → reusable utilities, core logic
- Function Expression → controlled execution, callbacks
- Arrow Function → small helpers, array methods, clean syntax
There’s no “best” — just the right tool for the situation.
🚨 Common Mistakes Developers Actually Make
❌ Overusing arrow functions everywhere
They’re great — but not always correct.
❌ Forgetting return in arrow functions
const sum = (a, b) => {
a + b; // ❌ undefined
};
Curly braces require return.
❌ Assuming all functions behave the same
Hoisting and this differences
matter.
🧪 Advanced Insight: Hoisting & Execution Order
Only function declarations are fully hoisted.
sayHi(); // ✅
function sayHi() {
console.log("Hi");
}
Function expressions and arrows are not callable before assignment.
This distinction prevents a lot of “why is this undefined?” bugs.
🧑💻 Personal Dev Take
In production code, I default to
arrow functions for small logic
and
function declarations for core utilities.
Being intentional with function types makes code reviews smoother and bugs
rarer.
🎯 Final Thoughts
Functions aren’t just syntax — they shape how your code is structured.
Once you understand the differences, you stop guessing and start writing JavaScript with confidence.
🚀 Best Practice Summary
✅ Use function declarations for core reusable logic
✅ Use function expressions for predictable execution
✅ Use arrow functions for concise callbacks
✅ Be careful with this in
arrow functions
✅ Don’t mix function styles without reason
0 Comments