QA
Senior LevelJavaScript & TypeScript

How do JavaScript execution contexts, lexical environments, and variable environments work under the hood?

❓ Interview Prompt: Explain Execution Context creation phase vs execution phase, Environment Records, and Outer Environment References.

Every piece of JavaScript code runs inside an Execution Context:

Execution Context Lifecycle:

1
Creation Phase:
•
Creates the LexicalEnvironment and VariableEnvironment.
•
Builds the Environment Record (allocates memory for functions and variables).
•
Sets the Outer Environment Reference (enables scope chain resolution).
•
Binds the this value.
2
Execution Phase:
•
Executes code line by line, evaluating expressions and assigning variable values.

Environment Record Components:

•
Declarative Environment Record: Stores let, const, class, and function declarations.
•
Object Environment Record: Stores var and global object properties.

Code Implementation Example

javascript
// Under-the-hood Execution Context conceptual model
/*
GlobalExecutionContext = {
  LexicalEnvironment: {
    EnvironmentRecord: {
      a: <uninitialized> (let),
      foo: <func>
    },
    outer: null,
    this: <globalObject>
  },
  VariableEnvironment: {
    EnvironmentRecord: {
      b: undefined (var)
    }
  }
}
*/

let a = 10;
var b = 20;
function foo() { return a + b; }
💡Internal structure representation of V8 Execution Context creation phase.
🎯 Interview Key Takeaway

Execution Contexts consist of Lexical and Variable Environments that track identifiers, outer scope links, and this binding.

#Execution Context#Engine#V8#Senior
Back to all JavaScript & TypeScript Questions→

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?