Function declaration vs function expression in JavaScript
BeginnerJavaScript interview questionBy UIRound, updated 17 November 2026
Short answer
A function declaration is a statement, function add(a, b) { ... }, that creates a named function when its scope starts, so you can call it above the line where it is written. A function expression creates a function as a value inside an expression, such as const add = function (a, b) { ... }. It exists only once that line runs, and it follows the hoisting rules of the variable it is assigned to.
The two forms
// Declaration: a statement that starts with the function keyword
function add(a, b) {
return a + b;
}
// Expression: the function is a value on the right of =
const subtract = function (a, b) {
return a - b;
};
// Also expressions: arrows, and functions passed as arguments
const multiply = (a, b) => a * b;
[1, 2, 3].map(function (n) { return n * 2; });The rule for telling them apart is position. If a line starts with the function keyword, it is a declaration. If the function appears anywhere a value is expected (after =, as an argument, in an object literal, after return, inside parentheses), it is an expression.
The hoisting difference
This is what interviewers are usually after.
console.log(add(2, 3)); // 5
function add(a, b) {
return a + b;
}A declaration is set up, body and all, before any code in its scope runs. Calling it earlier in the file works.
console.log(subtract(5, 2)); // ReferenceError: Cannot access 'subtract' before initialization
const subtract = function (a, b) {
return a - b;
};With an expression, only the variable is hoisted, and it follows its own rules. With const or let you get a ReferenceError from the temporal dead zone. With var the variable exists early but holds undefined, so calling it gives TypeError: subtract is not a function. The hoisting answer has the full table for every kind of declaration.
Comparison
| Function declaration | Function expression | |
|---|---|---|
| Syntax | function name() {} as a statement |
const name = function () {}, an arrow, a callback |
| Callable before its line | Yes | No: ReferenceError with let/const, TypeError with var |
| Name required | Yes (except export default function () {}) |
No, it can be anonymous |
| Can be reassigned later | Yes, the name is a normal variable | Not if you use const |
| Can be used inline as a value | No | Yes |
| Scope | The enclosing function; the enclosing block in strict mode and modules | Wherever its variable is declared |
Named function expressions
A function expression can have its own name, which is different from the variable it is stored in:
const factorial = function fact(n) {
return n <= 1 ? 1 : n * fact(n - 1);
};
factorial(5); // 120
typeof fact; // "undefined": the name only exists inside the function
factorial.name; // "fact"The inner name is visible only inside the function body. That makes recursion safe even if the outer variable is later reassigned, and it gives a clear name in stack traces. Inside the function the name is read-only: assigning to fact is silently ignored in sloppy mode and throws a TypeError in strict mode.
Anonymous expressions also get a name these days. In const subtract = function () {}, subtract.name is "subtract", because the engine infers it from the variable. So the stack-trace argument for naming matters less than it once did, but the recursion argument still holds.
Arrow functions
Arrow functions are always expressions. There is no arrow declaration, so const greet = () => "hi" follows the same hoisting rules as any const. Arrows also have no this, arguments or prototype of their own and cannot be called with new; the arrow functions answer covers those differences, which are separate from the declaration question.
Which should you use?
Both are fine, and teams pick a style. Some sensible defaults:
- Declarations for named, top-level helpers. You can put the main logic at the top of a file and the helpers below it, and the names are clear.
constwith an expression or arrow when you want the name never to be reassigned, when the function is created conditionally, or when it is passed around as a value.- Expressions for callbacks, object methods and IIFEs, where a declaration is not allowed anyway.
Avoid declaring functions inside if blocks. In strict mode and modules the function is scoped to the block, but in sloppy scripts browsers keep a legacy behaviour that can also expose it outside. If you need a function that depends on a condition, assign an expression to a variable declared outside the block.
Practise it
Predict hoisting and TDZ results and Predict which function declaration wins test the hoisting rules. Predict scope puzzles covers named function expressions, while Predict function results and Predict the output of loop, timer, promise, hoisting and closure snippets mix them with other basics.
Common follow-up questions
What happens if two declarations have the same name?
The last one wins. Both are hoisted, and the later body replaces the earlier one before any code runs, so even a call placed between them uses the second version. With let or const the same name twice is a SyntaxError.
Is const fn = () => {} hoisted?
const fn = () => {} hoisted?The variable fn is hoisted, but it is in the temporal dead zone until the line runs. Calling it earlier throws a ReferenceError.
Are class declarations hoisted like functions?
No. Class declarations behave like let: the name exists early but cannot be used before the declaration. Class expressions, const User = class {}, follow the variable's rules.
Do async functions and generators have both forms?
Yes. async function load() {} and function* ids() {} are declarations and hoisted the same way. const load = async () => {} and const ids = function* () {} are expressions.
Mistakes to avoid
Calling a function expression above its assignment and expecting it to work like a declaration
Thinking
var f = function () {}throws aReferenceErrorIt is a
TypeError, becausefisundefined.Believing the name of a named function expression is available outside it
Declaring functions inside blocks in sloppy-mode scripts
Saying arrow functions are "not hoisted"
The variable is hoisted; the function value is not assigned yet.
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict hoisting and TDZ results for functions,
var,let,constandclassEasy15 minFree - Predict which function declaration wins: duplicates, code after
returnand functions inside blocksEasy15 min - Predict scope puzzles: named function expressions, the
catchparameter, default-parameter scope and shadowingEasy25 min - Predict function results:
arguments, default parameters, rest,lengthand arrowsEasy20 min - Predict the output of loop, timer, promise, hoisting and closure snippetsEasy20 min