Function Declaration vs Function Expression in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
Introduction If you've ever built something with vanilla JavaScript and had to update the UI frequently, you already know the pain. You call document.getElementById, change some text, update a class,
Introduction If you've ever installed and tried out Linux after seeing a cool hacking sequence in a sci-fi movie, you know that opening it feels like stepping into a strange city with many roads named

Introduction Most JavaScript confusion around this comes from one wrong assumption: that this depends on where a function is defined. It doesn't. It depends on who called the function. That one shift

Introduction JavaScript gives you objects and arrays out of the box, and for a long time those two were the default answer to almost every data storage problem. Objects for key-value pairs, arrays for

Introduction Most JavaScript codebases are full of code like this: const user = { name: "Sara", age: 28, city: "Mumbai" }; const name = user.name; const age = user.age; const city = user.city; Three

If you've been writing JavaScript for a while, you've probably used both without thinking much about which one you're reaching for. They look similar, and for basic cases, they behave similarly too. But they're not the same thing, and the difference actually matters once your code grows past a few lines.
A function is a block of code you can name and reuse. Without functions, you'd write the same logic everywhere it's needed. With functions, you write it once and call it whenever.
That's it. No mystery. Here's the clearest example:
// Without a function
let result1 = 5 + 3;
let result2 = 10 + 3;
let result3 = 7 + 3;
// With a function
function addThree(num) {
return num + 3;
}
let result1 = addThree(5);
let result2 = addThree(10);
let result3 = addThree(7);
If the logic changes later, you fix it in one place instead of hunting through every file.
A function declaration is what most people learn first. You write the function keyword, give it a name, and define the body.
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // 5
Clean and readable. The function has a name (add), takes two parameters, and returns their sum.
A function expression is when you assign a function to a variable.
const add = function(a, b) {
return a + b;
};
console.log(add(2, 3)); // 5
Same behavior here. You're still calling add(2, 3) and getting 5. The difference is in how the function was created, not what it does.
You can also use arrow function syntax, which is just a shorter way to write a function expression:
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
Arrow functions are common in modern JavaScript. They're still function expressions, just written more compactly.
This is where things actually diverge.
JavaScript does something called hoisting before running your code. It scans the file and moves certain declarations to the top. Function declarations get fully hoisted, meaning the browser knows about the entire function before any code runs.
So this works:
console.log(add(2, 3)); // 5 -- called before the function is defined
function add(a, b) {
return a + b;
}
You called add before it was written in the file, and JavaScript didn't complain. That's hoisting in action.
Now try the same thing with a function expression:
console.log(add(2, 3)); // Error: Cannot access 'add' before initialization
const add = function(a, b) {
return a + b;
};
This throws an error. The variable add is hoisted (JavaScript knows it exists), but its value isn't assigned yet when you try to call it. You have to define it before using it.
The short version: declarations are available anywhere in their scope, expressions are only available after the line where they're defined.
| Point | Function Declaration | Function Expression |
|---|---|---|
| Syntax | function name() {} |
const name = function() {} |
| Hoisted | Yes (fully) | No (only the variable) |
| Can call before definition | Yes | No |
| Named | Always | Optional |
Use a function declaration when you're writing utility functions or anything that should be available throughout a file. The hoisting behavior means you can organize your file however reads best, putting helper functions at the bottom even if the main logic uses them higher up.
// This is fine -- greet is hoisted
greet("Sam");
function greet(name) {
console.log("Hello, " + name);
}
Use a function expression when you need to assign a function dynamically, pass it as an argument, or return it from another function. Expressions treat functions as values, which is what makes callbacks and higher-order functions work.
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(function(n) {
return n * 2;
});
// Or with arrow syntax
const doubled = numbers.map(n => n * 2);
You'd never write a function declaration inline like that. Expressions fit naturally wherever a value would go.
Arrow functions specifically are worth using when you don't need your own this context (they inherit this from the surrounding scope). For callbacks, array methods, and short transformations, they're the go-to.
If you're defining a standalone, named function that you'll reuse across your file, go with a declaration. If you're passing a function somewhere, storing it in a variable, or building it conditionally, go with an expression.
Neither is universally better. The right choice comes down to what your code is doing and what makes the intent clear to whoever reads it next.