Use an inner function that remembers outer values.

Nested Closures

nested_closure.js
const factor = 3;

function multiplyWithFactor(value) {
  function multiply() {
    return value * factor;
  }
  return multiply();
}

const result = multiplyWithFactor(4);

console.log("factor=" + factor);
console.log("result=" + result);
nested-closure A nested function can read variables from both its own function and the outer scope. Calling it immediately keeps the replay state scalar.