Prep Day-1: JavaScript Core with (React + Angular)
JavaScript Fundamentals — How JS Actually Works
Before React, Angular, or any framework — interviews test your JavaScript foundation.
This whole series is not for begineer, Its expected you should have knowledge and some experience in Javascript, Angular/React
We will go gradually from zero, and I will try to jump right into the topic.
For core JS I will do a small recap and then I will show some real world usecase in Angular and React
Let's revise some of the basic JS core concepts
Most developers learn JavaScript by usage, not by understanding.
That works — until interviews ask:
Senior frontend interviews don’t test what you write, they test what you understand.
JavaScript Runtime: The Big Picture
JavaScript is a single-threaded, synchronous language — but it runs in an environment.
JavaScript Runtime consists of:
JavaScript itself only knows stack + heap. Async behavior comes from the runtime environment, not JS itself.
JS Engine: How Code is Executed
Step-by-step:
Each execution context has:
Execution Context (EC)
An Execution Context is the environment in which JavaScript code runs.
Whenever JS runs any code, it does inside an execution context
Think it of as a box that contains:
Types:
Phases:
1. Memory Creation Phase
console.log(a) //undefined
var a = 10
2. Execution Phase
var a = 10;
function foo() {
var b = 20;
console.log(a + b);
}
foo();
What happens internally:
Another example:
var globalVar = 'Hello'; // Global Context
function greet(name) { // Function Context for greet()
let localVar = 'World';
console.log(globalVar + ', ' + name + ' ' + localVar);
innerFunction(); // New Function Context for innerFunction()
}
function innerFunction() { // Function Context for innerFunction()
console.log('Inside inner.');
}
greet('User'); // Invokes greet(), creating its Function EC
LEXICAL ENVIRONMENT
A Lexical Environment is a structure that holds:
It defines where variables and functions are accessible in code.
Structure of a Lexical Environment
Each Lexical Environment has two parts:
Lexical Environment = {
Environment Record,
Outer Lexical Environment Reference
}
Types of Lexical Environments
1. Global Lexical Environment
2. Function Lexical Environment
3. Block Lexical Environment
Lexical Scope
Variables are resolved based on where the function is written, not where it’s called.
let x = 100;
function foo() {
console.log(x);
}
function bar() {
let x = 200;
foo();
}
bar();
Output : 100
Why?
SCOPE CHAIN
When a variable is used, JavaScript looks for it in the current scope, and if not found, it keeps moving outward through parent lexical environments until it finds it or reaches null.
Scope Chain = Current Scope → Parent Scope → Global Scope → null
How Scope Chain Works Internally
Scope Chain is created using:
Each scope knows where it was defined, not where it is executed.
let a = 10;
function outer() {
let b = 20;
function inner() {
let c = 30;
console.log(a, b, c);
}
inner();
}
outer();
Scope Chain Lookup
inner → outer → global → null
Output:
10 20 30
Call Stack: How JS Manages Execution
Call Stack = Stack data structure that tracks execution context. It follows LIFO (Last In First Out)
Rules:
Example:
function a() {
b();
}
function b() {
c();
}
function c() {
console.log("Hello");
}
a();
In stack it will be like this
[
c()
b()
a()
]
Stack Overflow:
A function calling itself
function infinite(){
infinite()
}
infinite();
Real-World Use Case
Understanding execution context helps you:
This is critical in:
Recommended by LinkedIn
Optimization Tips
Common performance mistakes:
Solutions:
Use Case in React
Each render creates a new execution context
function Counter() {
const [count, setCount] = useState(0);
console.log('render');
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
Every time React re-renders this component, it calls this function again from scratch.
That means:
A brand-new function execution happens on every render
This new execution is what we call a new execution context.
Every render:
A common issue in React:
❌ Why does my variable reset on re-render?
function Timer() {
let seconds = 0;
setTimeout(() => {
seconds++;
console.log(seconds);
}, 1000);
return null;
}
❌ Bug seconds resets on every render because it lives in the execution context.
Fix with state or ref
const [seconds, setSeconds] = useState(0);
or
const secondsRef = useRef(0);
Think of React renders like this:
Render #1 → Execution Context #1
Render #2 → Execution Context #2
Render #3 → Execution Context #3
❌ Nothing is shared unless we use any of the following hooks:
Excessive re-render -> Stack pressure
This is why use:
not to “optimize everything”, but to control execution.
Use Case in Angular
1.) Does Change Detection runs inside execution contexts ?
Yes — but indirectly.Angular change detection runs as a result of JavaScript execution contexts, not as a separate thread or engine
Every time JS runs:
Angular lives inside this JavaScript execution model.
Angular uses Zone.js to patch async APIs like:
Zone.js detects when an async task finishes.
When Does Change Detection Run?
After an async task completes and control returns to JS:
Async task finishes
→ JS execution context completes
→ Zone.js notifies Angular
→ Angular runs Change Detection
Change Detection is triggered after JavaScript execution contexts complete, not inside the context itself.
Performance:
Because change detection is tied to JS execution:
This explains why:
React and Angular execution behavior.
System Design Perspective
When designing frontend systems:
REACT System Design Perspective:
1) Render = Stateless Function
Think serverless function invocation
2) Performance Design
Stabilize interfaces, not everything (React.memo, useCallback, useMemo only where needed)
ANGULAR System Design Perspective:
In large Angular systems:
This is frontend execution management at scale.
INTERVIEW QUESTIONS
What is an execution context?
Explain memory creation vs execution phase
How does the call stack work?
How does execution context relate to closures?
Relationship between Execution Context and Call Stack
REACT BASED :
Q1) Every time a React component renders, does a new JavaScript execution context get created?
My tips: Try to include this points
Q2) Why do inline functions cause unnecessary re-renders?
Q3) How does React rendering interact with the JavaScript call stack?
Q4) Why does React require hooks to be called at the top level?
Q5) How can a React app cause a stack overflow?
My tips: Try to include this points
Q6) How does understanding execution context help optimize React apps?
My tips: Try to include this points:
ANGULAR BASED:
Q1) How does JavaScript execution affect Angular change detection?
Q2) What role does Zone.js play in Angular execution? (Not for new Angular with zoneless)
Q3) Why does every click trigger change detection in Angular?
My tips: Try to include this points:
Thats it for Day1. Try to focus in this topics and try to solve the answers.
If you want some more example please comment below, Also please give feedback if anything you want to change.
Every feedback is welcomed.
Excellent prep material
Awesome stuff!!