closure

A function bundled with the variables it remembers from the scope where it was created.

In more detail

When an inner function uses variables of its enclosing function and outlives that call, those variables stay alive with it. Each call to the enclosing function makes a separate closure, so a factory can produce many functions with different remembered values. Closures look their variables up when they run, not when they are created.

Example

Every function created in a React component captures that render props and state; a closure kept across renders becomes a stale closure.

Related terms

Taught in

References

Certification courses