Function Declarations vs Expressions
A function declaration is a statement that creates a named function and hoists it. A function expression produces a function value as part of another expression, and that value exists only after the line runs.
Detailed Explanation
The two forms look similar and behave differently at the start of a scope. That difference is why one call-before-define works and the other throws TypeError: ... is not a function or ReferenceError.
A declaration is:
function parse(input) {
return input.trim();
}
It is a statement. The engine creates the function object while instantiating the scope, before any line of that scope runs. You can call parse on the line above its definition. The name parse is local to the enclosing scope (or global, at the top level). Inside the function, parse refers to itself, which is how recursion works without an extra binding.
A function expression is a function used where a value is expected:
const parse = function (input) {
return input.trim();
};
Or the named form const parse = function parse(input) { ... }. The const (or let, or var) is hoisted as a binding, but the assignment happens when that line runs. Call parse before that line and you are in the temporal dead zone for let/const, or you get undefined for var and then a TypeError when you try to invoke it.
The optional name on a function expression is local to the function body. It shows up in stack traces and lets the function call itself. It does not create a binding in the outer scope. const outer = function inner() {} — inner is not visible outside.
Arrow functions are always expressions. There is no arrow declaration. Method shorthand inside an object literal ({ parse(input) {} }) is a third form: a method definition, not hoisted as a free function.
A declaration inside a block (if (flag) { function helper() {} }) is hoisted to the block in strict mode, and historically leaked in sloppy mode in some engines. Do not declare functions inside blocks. Use a const expression instead, so the binding is obviously block-scoped.
Reach for a declaration when a named, hoisted helper makes the file easier to read top-down. Reach for an expression when the function is a value you pass, assign conditionally, or export as a const. Many style guides prefer expressions for consistency with const and arrows; both are correct.
What it costs you: treating them as the same thing, then hitting TDZ or var undefined when you reorder the file.
Code Examples
Declaration: callable before its line.
// Declaration: callable before its line.
console.log(area(3, 4)); // => 12
function area(width, height) {
return width * height;
}
// Expression: the binding exists, the value does not, until this line runs.
try {
console.log(volume(2, 3, 4));
} catch (err) {
console.log(err.name); // => ReferenceError (TDZ for const)
}
const volume = function (w, h, d) {
return w * h * d;
};
console.log(volume(2, 3, 4)); // => 24Named expression: the inner name is for recursion and stack traces only.
// Named expression: the inner name is for recursion and stack traces only.
const factorial = function fact(n) {
if (n <= 1) return 1;
return n * fact(n - 1);
};
console.log(factorial(5)); // => 120
console.log(typeof fact); // => undefinedvar + expression: hoisted as undefined, then assigned.
// var + expression: hoisted as undefined, then assigned.
console.log(typeof later); // => undefined
var later = function () {
return "ready";
};
console.log(later()); // => readyCommon Gotchas
Calling an expression before assignment fails
`const fn = function () {}` is not hoisted as a function. You get `ReferenceError` (`let`/`const`) or `TypeError` (`var`).
Function declarations inside `if`/`else` are engine-dependent in sloppy mode
write `const helper = function () {}` inside the block instead.
A named function expression does not leak its name
`const f = function g() {}` leaves `g` invisible outside `g`'s body.
`export default function name() {}` is a declaration
it is hoisted within the module. `export default function () {}` is an anonymous default export.