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
- memory leak — Memory that a program keeps holding although it no longer needs it, so usage grows until the program slows down or crashes.
- callback — A function you pass to other code so it can call you back later, for example when an event happens or a timer fires.
- stale closure — A function that still sees old values because it was created during an earlier render and never replaced.
Taught in
- Memory Management: Garbage Collection, Memory Leaks And WeakMap — Frontend Architecture & Web Platform
- State with useState: Immutability And Asynchronous Batching — React 19 & Modern Component Architecture
- SyntheticEvent System: Event Delegation in Modern React — React 19 & Modern Component Architecture
- Rules of Hooks: Call Order, Hook Lists, And Fiber Storage — React 19 & Modern Component Architecture
- Creating Event Listener Hooks with Clean Teardown Logic — React 19 & Modern Component Architecture
- React.memo, useMemo, And useCallback: Optimization Costs — React 19 & Modern Component Architecture
- The React Compiler: Automated Memoization Under The Hood — React 19 & Modern Component Architecture
- The Class That Shared One List — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Sixty Five Minutes, Forty Items: How PCAP Is Run — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Closures: Functions That Remember — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep