Variables
Variable Environments
In JavaScript, every execution context (like global code, a function, or a block) has an associated Variable Environment.
A Variable Environment is a special internal object where all the variables and function declarations of that context are stored.
When JavaScript code runs:
- The Execution Context is created.
- Inside it, the Variable Environment is created to keep track of variables defined with
var,let, andconst. - This environment helps JavaScript engines know where and how to find variables during execution.
Components of Variable Environment
The Variable Environment is closely related to Lexical Environments. Both together form the scope chain.
- Variable Declarations (var, let, const)
- Stored differently:
- `var → Stored in Variable Environment (function-scoped).
let &const` → Stored in Lexical Environment (block-scoped), but conceptually part of execution context too.
- Function Declarations
- Stored in Variable Environment and hoisted.
- Scope Chain
- Variable Environment links to its outer environment reference, allowing inner functions to access variables from outer scopes (closures).
Variable Environment vs Lexical Environment
- Variable Environment:
- Holds variables declared with var (function-scoped).
- Created when execution context is created.
- Lexical Environment:
- Holds let and const (block-scoped).
- Depends on where code is written (lexical scope).
📌 But in modern JS discussions, people often use Lexical Environment as a more general term. The Variable Environment is just a specific part of it.
Variable Declaration
-
varmaintain function scope (not global scope),let,constmaintain block scope. -
Updating/re-assigning
constvariable createTypeError: Assignment to constant variable.error. -
varmaintain function scope,let,constmaintain block scope. -
Updating/re-assigning
constvariable createTypeError: Assignment to constant variable.error.const a = "hello";
a = "hi";-
It creates a constant reference to a value. if the value is an object or array, you can mutate its contents (because the reference stays the same).
const x = 10;
x = 20; // ❌ Error: Assignment to constant variable
const arr = [1, 2, 3];
arr.push(4); // ✅ Allowed
arr = [5, 6]; // ❌ Error -
Primitive
const,let,varstore in stack, Non-primitive store in stack.
-
-
A variable with same name can be declare twice with
varbut not withletandconst, it will createSyntaxError: Identifier 'a' has already been declared.-
It creates a constant reference to a value. if the value is an object or array, you can mutate its contents (because the reference stays the same).
const x = 10;
x = 20; // ❌ Error: Assignment to constant variable
const arr = [1, 2, 3];
arr.push(4); // ✅ Allowed
arr = [5, 6]; // ❌ Error -
Primitive
const,let,varstore in stack, Non-primitive store in stack.
-
-
A variable with same name can be declare twice with
varbut not withletandconst, it will createSyntaxError: Identifier 'a' has already been declared.let a='hello'; // ERROR
let a="hi"; // ERROR
var b="hello";
var b="hello";a variable with same name can be declare twice with
letif both are in different scope, this is not applicable forconst.let a = 5;
console.log(a);
{
let a = 4;
console.log(a);
}
console.log(a); -
varmaintain function/global scope,letandconstmaintain local scope. -
Using any variable before declaration with
varreturnundefineddue to hoisting but withletandconstit will return an error ofReferenceError: Cannot access 'myVariable' before initializationdue to temporal deadzone.console.log(myVariable);
let myVariable = "Hello";
ReferenceError: Occurs when a variable that isn’t declared or isn’t accessible is referenced. This often happens due to misspellings, accessing variables in the temporal dead zone, or outside their scope.
SyntaxError: Occurs when code does not conform to the correct syntax of the language. This type of error is detected before the code is executed and typically involves missing or incorrect syntax elements.
TypeError: Occurs when a value is not of the expected type, such as calling a non-function as a function, or accessing properties on null or undefined.
Explaination:
let i = 50;
for (let i = 0; i < 5; i++) {
console.log(i);
}
console.log(i);
- memory allocate for initial
iin script object with global scope - memory allocate for last
iin script object with local scope - memory allocate for initial
iin script object with global scope - memory allocate for last
iin script object with local scope
Automatic Global Variable
- We can declare variable without
var, in that case it will be consider as global variable. - Scope is determine in creation phase, but undeclared variable executed at exection phase, that's how it became global variable
- this will not in strict mode
const show = () => {
name = "Masum Billah";
};
show();
console.log(name);
{
age = 25;
}
console.log(age);
## Automatic Global Variable
- We can declare variable without `var`, in that case it will be consider as global variable.
- Scope is determine in creation phase, but undeclared variable executed at exection phase, that's how it became global variable
- this will not in strict mode
```javascript
const show = () => {
name = "Masum Billah";
};
show();
console.log(name);
{
age = 25;
}
console.log(age);