Assigned value
The function is stored in a variable.
Function expressions create functions as values. They can be assigned to variables, passed around and used where expressions are expected.
const formatTitle = function (value) {
return value.trim();
};
console.log(formatTitle(" Ready "));
Functions & Scope
A function expression creates a function and places it inside an expression. Most commonly, it is assigned to a const variable.
Unlike function declarations, function expressions are not callable before the assignment has run. The variable exists according to normal let or const rules.
Function expressions are useful for callbacks, configuration objects, event handlers and cases where a function belongs to a value rather than the top-level flow.
The function is stored in a variable.
The function can have no name or an internal name for stack traces.
The variable must be initialized before calling it.
The function can be passed as data.
Examples
const createLabel = function (status) {
return `Status: ${status}`;
};
console.log(createLabel("ready"));
console.log(createLabel("ready"));
const createLabel = function (status) {
return `Status: ${status}`;
};
Code patterns
These examples focus on syntax you will actually look up later: declarations, callbacks, returns, closures and context handling. Use the small patterns first, then study the deeper explanation.
Store behavior in a const.
const double = function (value) {
return value * 2;
};
console.log(double(4));
A name can improve stack traces and recursion.
const factorial = function calculate(value) {
if (value <= 1) return 1;
return value * calculate(value - 1);
};
console.log(factorial(5));
Store behavior next to related settings.
const formatter = {
label: function (value) {
return `Status: ${value}`;
}
};
console.log(formatter.label("ready"));
Callbacks are built on function values.
const numbers = [1, 2, 3];
const doubled = numbers.map(function (value) {
return value * 2;
});
console.log(doubled);
Rules that matter
Functions are where JavaScript stops being a list of statements and becomes a system. Scope decides what each function can see, while return values and parameters decide how functions communicate.
The function binding usually should not be reassigned.
Function expressions follow variable initialization rules.
The variable name often carries the function meaning.
They can improve recursion and stack traces.
Large anonymous functions are hard to scan.
Use expressions when value-like behavior is useful.
Production thinking
Function expressions are the gateway to callbacks, array methods, event handlers and flexible code organization.
Event handlers and UI callbacks often use function expressions. Clear names make behavior easier to audit.
Production code should avoid huge inline anonymous functions and extract important behavior into named constants.
Rendering callbacks that build content should be small and predictable, especially when they affect visible text or links.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Functions only become useful when you can explain what goes in, what comes out and which scope the function can see.