Variable Environment in JavaScript Explained
Each function call has its own variable environment. Here is what it holds and how it differs from the scope chain.
Variable Environment in JavaScript Explained
The variable environment is the container that holds a function's local variables and declarations. It is part of every execution context.
What It Contains
- Function parameters: bound to the arguments passed in.
vardeclarations: hoisted withundefined, assigned during code execution.letandconstdeclarations: hoisted but in the TDZ until the declaration line.- Inner function declarations: stored in full during the memory phase.
argumentsobject (in regular functions only): array-like, holds all passed arguments.
Per-Call, Not Per-Function
A new variable environment is created every time the function is invoked. Two calls to the same function do not share locals.
function counter() { var count = 0; return ++count; } counter(); // 1 counter(); // 1, not 2
Each call has its own count, starting at 0. To persist state, use a closure.
Variable Environment vs Scope Chain
- Variable environment: the locals of the current call.
- Scope chain: the chain of outer environment references used to resolve variables not found locally.
The scope chain is built lexically (based on where functions are written), not dynamically (based on who called what).
Example
const globalVar = "G"; function outer() { var outerVar = "O"; function inner() { var innerVar = "I"; console.log(innerVar, outerVar, globalVar); // I, O, G } inner(); } outer();
When inner runs, its variable environment has innerVar. The scope chain is inner -> outer -> global, so outerVar and globalVar resolve by walking up.
Hoisting Lives Here
Hoisting is the act of populating the variable environment during the memory phase. var becomes undefined, function declarations become full functions, let/const sit in the TDZ.
The Takeaway
The variable environment is the per-call container for a function's locals: parameters, var, let, const, inner functions, and arguments. It is populated during the memory phase (hoisting). The scope chain resolves variables not found in the local environment by walking up lexical parents.
The container that holds the function's local state for a given call: parameters, var/let/const declarations, inner function declarations, and the arguments object. A new one is created per invocation.
No. Each invocation creates a fresh variable environment. Local variables are not shared between calls. To persist state across calls, use a closure.
The variable environment holds the current call's locals. The scope chain is the chain of outer environment references used to resolve variables not found locally, built lexically at write time.
Yes, in regular (non-arrow) functions. The arguments object is created during the memory phase and holds all passed arguments. Arrow functions do not have their own arguments object.
Hoisting is the act of populating the variable environment during the memory phase. var becomes undefined, function declarations are stored in full, and let/const are placed in the TDZ.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

