JavaScript Execution Context Kya Hota Hai?
Samjhein ki parde ke peeche aapka JavaScript code kaise execute hota hai.
Execution Context ki Definition
JavaScript mein, Execution Context ek abstract environment hota hai jahan par aapka code evaluate aur execute hota hai. Jab bhi JavaScript engine koi code run karta hai, woh sabse pehle ek Execution Context create karta hai. Yeh context code ko run karne ke liye zaroori sabhi information, jaise variables, functions, aur scope, ko manage karta hai.
Types of Execution Contexts
JavaScript mein mukhya roop se 2 tarah ke execution context hote hain:
1. Global Execution Context (GEC)
Jab browser ya Node.js aapke script ko pehli baar load karta hai, toh ek Global Execution Context (GEC) create hota hai. Yeh base context hota hai aur isme woh saara code run hota hai jo kisi function ke andar nahi likha gaya hai. Ek program me sirf ek hi GEC hota hai.
var name = "Sandeep"; // Yeh GEC me execute hoga
console.log(name);
2. Function Execution Context (FEC)
Jab bhi koi function call hota hai, tab ek naya Function Execution Context (FEC) create hota hai. Har function call ke liye ek alag FEC banta hai.
function greet() {
var message = "Hello!"; // Yeh FEC me execute hoga
console.log(message);
}
greet(); // Is call par naya FEC banega
Execution Context ke Do Phases
Har Execution Context do phases me banta hai:
Phase 1: Creation Phase (Memory Allocation)
Is phase me, JavaScript engine code ko execute nahi karta, balki uske liye memory prepare karta hai.
- Memory for Variables: `var` se declare kiye gaye variables ke liye memory allocate hoti hai aur unhe `undefined` value assign ki jaati hai (ise hoisting kehte hain). `let` aur `const` bhi hoist hote hain, par unhe access nahi kiya ja sakta.
- Memory for Functions: Function declarations ke definitions ko poora memory me store kiya jaata hai.
- `this` Keyword Binding: `this` keyword ki value set hoti hai (GEC me `window` object).
Phase 2: Execution Phase
Is phase me, JavaScript engine code ko line-by-line execute karta hai.
- Variables ko unki actual values assign ki jaati hain.
- Functions ko call kiya jaata hai aur code run hota hai.
console.log(a); // Creation phase me 'a' undefined hai
var a = 10; // Execution phase me 'a' ko 10 assign hoga
console.log(a); // Execution phase me 'a' ki value 10 hai
Call Stack aur Execution Context
JavaScript single-threaded language hai, isliye woh ek baar me ek hi kaam kar sakta hai. Call Stack (jise Execution Stack bhi kehte hain) ek LIFO (Last-In, First-Out) data structure hai jo function calls ko track karta hai.
- Jab script run hoti hai, GEC call stack me push ho jaata hai.
- Jab koi function call hota hai, uska FEC create hota hai aur stack ke top par push ho jaata hai.
- Jab function apna kaam poora kar leta hai, toh uska FEC stack se pop (remove) ho jaata hai.
function first() {
console.log("Entering first()");
second();
console.log("Exiting first()");
}
function second() {
console.log(" Entering second()");
console.log(" Exiting second()");
}
first();
Is example ka execution flow call stack me is prakaar hoga:
- `first()` call hota hai, `first()` ka FEC stack me push hota hai.
- `first()` ke andar `second()` call hota hai, `second()` ka FEC stack me push hota hai.
- `second()` complete hota hai, `second()` ka FEC stack se pop hota hai.
- `first()` complete hota hai, `first()` ka FEC stack se pop hota hai.
Key Takeaways
- Execution Context woh environment hai jahan JS code run hota hai.
- Do mukhya types hain: Global (GEC) aur Function (FEC).
- Har context Creation Phase (memory allocation) aur Execution Phase (code run) se guzarta hai.
- Call Stack function calls ko LIFO (Last-In, First-Out) order me manage karta hai.
- Yeh concepts Hoisting aur Scope jaise doosre core concepts ka base hain.
Ek function ke andar doosra function call karein aur `console.log()` ka use karke dekhein ki execution ka order kya hota hai.
Practice in JS EditorApni knowledge test karne ke liye is quick quiz ko dein.
Start Quiz