Course contentsShow
← JavaScript
Lesson 504 of 2,72313. ClosuresFree lesson

Closures Over Parameters

Function parameters are part of the function's scope and are captured by inner functions just like local variables.

Closures Over Parameters

You'll learn that function parameters live in the function's scope and can be captured by closures, just like regular local variables.


When you define a function, its parameters are part of its local scope — and that means inner functions can close over them exactly like any variable declared inside the function body.

Parameters Are Variables Too

Think of parameters as variables that are automatically declared and initialized with the values you pass in. When an inner function references a parameter, it captures that parameter in its closure:

function makeGreeting(greeting) {
  return function(name) {
    return `${greeting}, ${name}!`;
  };
}

const sayHello = makeGreeting("Hello");
const sayHi = makeGreeting("Hi");

console.log(sayHello("Alice"));  // "Hello, Alice!"
console.log(sayHi("Bob"));       // "Hi, Bob!"

Each call to makeGreeting creates a new closure. The inner function remembers the specific greeting parameter value from its outer function call.

Multiple Parameters, Same Rule

All parameters follow this pattern:

function createMultiplier(factor, offset) {
  return function(value) {
    return (value * factor) + offset;
  };
}

const double = createMultiplier(2, 0);
const doublePlusOne = createMultiplier(2, 1);

console.log(double(5));         // 10
console.log(doublePlusOne(5));  // 11

The inner function closes over both factor and offset, preserving them for future calls.

Why This Matters

Parameters are often used to configure behavior — closing over them lets you create customized functions without hard-coding values.

Key Takeaway: Function parameters are captured by closures just like local variables — each inner function remembers the specific parameter values from its outer function call.