A JavaScript function expression is a function defined inside an expression, usually assigned to a variable: const add = function (a, b) { return a + b; };. Unlike a function declaration (function add(a, b) { ... }), a function expression is not hoisted, so you can only call it after the line that creates it runs.
The two forms use the same function keyword. What decides which one you have is the position: if the code is a statement on its own, it is a declaration. If it appears anywhere an expression is allowed, it is a function expression.
Function expression syntax
A function expression can be anonymous or named:
// anonymous
const square = function (n) {
return n * n;
};
// named
const factorial = function fact(n) {
return n <= 1 ? 1 : n * fact(n - 1);
};
console.log(square(4)); // 16
console.log(factorial(5)); // 120
MDN notes that an expression statement cannot begin with function, because the parser would read it as a declaration. The keyword starts an expression only in places where a statement is not accepted, such as the right side of =, a function argument, or inside parentheses.
The final semicolon after the closing brace is part of the const statement. A declaration does not need one.
Function declaration syntax
function square(n) {
return n * n;
}
A declaration must have a name, and that name is added to the surrounding scope. You can call it from anywhere in that scope, including above the declaration itself.
Function expression vs declaration: the differences
| Declaration | Expression | |
|---|---|---|
| Where it appears | As a statement | Wherever an expression is allowed |
| Name | Required | Optional |
| Hoisting | Hoisted with its value | Not hoisted |
| Callable before its line | Yes | No (TypeError with var, ReferenceError with let or const) |
| Name visible in outer scope | Yes | No, only inside the function body |
| Can be passed or invoked in place | Not directly | Yes |
Hoisting: the difference that causes bugs
MDN says function declarations are "hoisted together with its value and can be called anywhere in its scope." This works:
hoisted(); // logs "foo"
function hoisted() {
console.log("foo");
}
A function expression is not hoisted. What happens when you call it early depends on how you declared the variable that holds it. With var, the variable name is hoisted but its value is not, so it is undefined until the assignment runs. This is MDN's example:
console.log(notHoisted); // undefined
notHoisted(); // TypeError: notHoisted is not a function
var notHoisted = function () {
console.log("bar");
};
With let or const, the variable is in its temporal dead zone until the declaration line runs, and any access before that throws a ReferenceError. MDN's Hoisting page describes this as strictly forbidding any use of the variable before its declaration. The exact message text depends on the JavaScript engine, so read the one your runtime prints.
greet(); // ReferenceError, because greet is still in its temporal dead zone
const greet = function () {
console.log("hello");
};
The practical rule: if other code in the file calls a helper before the place where you wrote it, a declaration works and an expression does not. If you define everything above its first use, both work.
Named function expressions
Giving a function expression a name has three effects. The function can call itself by that name, the name becomes the function's name property, and the name shows up in debugging output. The name exists only inside the function body:
const math = {
factorial: function factorial(n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
},
};
console.log(math.factorial(3)); // 6
console.log(typeof factorial); // "undefined", the name is not in the outer scope
MDN states that if a function expression is named, its name property is that name, "instead of the implicit name inferred from syntax (such as the variable the function is assigned to)". It also notes that, unlike a declaration, the name of a function expression is read-only. In strict mode, assigning to it inside the function throws a TypeError.
Anonymous function expressions and callbacks
Most function expressions you see in real code are anonymous and passed straight into another call:
button.addEventListener("click", function (event) {
console.log("clicked");
});
[1, 2, 3].map(function (n) {
return n * 2;
}); // [2, 4, 6]
This is the main reason function expressions exist. You write the function exactly where it is used, without inventing a name and a separate statement for it. Today the same callbacks are more often written as arrow functions, which are covered below.
Immediately invoked function expressions (IIFE)
Because a function expression is an expression, you can call it at once by wrapping it in parentheses and adding a call:
(function () {
const secret = "only visible in here";
console.log(secret);
})();
(function (a, b) {
console.log(a + b);
})(1, 2); // logs 3
The parentheses force the parser to treat function as the start of an expression rather than a declaration. IIFEs were widely used to keep variables out of the global scope in classic scripts. MDN says modules and block-scoped let and const have made many of those uses unnecessary. They are still handy when you need a block of logic in an expression position, such as initializing a const with a try...catch.
Arrow functions are function expressions too
An arrow function is also an expression, so assigning one to const gives you the same no-hoisting behavior:
const double = (n) => n * 2;
Arrow functions are not a drop-in replacement for function expressions, though. According to MDN, an arrow function has no binding to this, arguments or super, it has no prototype property, and it throws a TypeError when called with new. Use a regular function expression or declaration when you need your own this (for a method or constructor) or the arguments object.
Block-level declarations behave differently in strict mode
One more difference matters if you declare a function inside an if or a loop body. MDN warns that in non-strict mode "function declarations inside blocks behave strangely", and that results are inconsistent across implementations. In strict mode, a block-level declaration is scoped to that block and hoisted to the top of the block. If you want a function that exists only under some condition, assign a function expression to a variable instead of declaring it in the block:
"use strict";
let format;
if (useShort) {
format = function (value) { return String(value); };
} else {
format = function (value) { return value.toFixed(2); };
}
Which one should you use?
- Top-level helpers that are used across a file: declarations. They can be called from anywhere in their scope, and the name appears in stack traces.
- Callbacks and values you pass around: expressions, usually arrow functions.
- Functions chosen at run time: expressions assigned to a variable.
- Recursion inside an expression: a named function expression.
Neither form is faster or more correct in general. The deciding factors are hoisting, whether the function needs a name in the outer scope, and where you want to write it. For the broader idea of what a function is, see what a function is in programming.
If you get "is not a function"
The error TypeError: x is not a function after defining x as an expression usually means one of three things. You called it before the assignment ran (the variable is still undefined under var). The variable was reassigned to something else later. Or a different variable with the same name in an inner scope is shadowing it. Move the definition above its first use, or switch to a declaration if the order matters.
Frequently asked questions
- What is a function expression in JavaScript?
- A function expression is a function defined where JavaScript expects an expression, for example on the right side of an assignment: const square = function (n) { return n * n; };. It can be anonymous or have its own name, and unlike a function declaration it is not hoisted.
- Can I call a function expression before it is defined?
- No. A function expression assigned with const or let throws a ReferenceError if you call it before the line that assigns it. With var, the variable exists but holds undefined, so the call throws a TypeError saying it is not a function. A function declaration can be called anywhere in its scope.
- Is an arrow function a function expression?
- Yes, an arrow function is an expression, so it follows the same hoisting rules as a function expression assigned to a variable. It differs in other ways: it has no own this or arguments, it has no prototype property, and it cannot be called with new.
- Why give a function expression a name?
- A name lets the function call itself recursively, and it appears in stack traces and in the function's name property. The name is only visible inside the function body, not in the surrounding scope.