🖥️

JavaScript का Execution Context

Aug 5, 2025

Overview

यह लेक्चर JavaScript में कोड के execution context, उसके phases और call stack के काम करने के तरीके को विस्तार से समझाता है।

Execution Context क्या है?

  • Execution Context वह वातावरण है जिसमें JavaScript कोड execute होता है।
  • JavaScript हर कोड को दो phases में run करती है: Memory Creation Phase और Execution Phase।
  • सबसे पहले Global Execution Context बनता है, जिसे browser में window object द्वारा refer किया जाता है।
  • Environment के हिसाब से (browser, node.js) global execution context अलग हो सकता है।

Execution Context के Types

  • दो प्रमुख प्रकार होते हैं: Global Execution Context और Functional Execution Context।
  • हर function call पर नया functional execution context बनता है।
  • एक तीसरा eval execution context भी होता है, जो खास global object की property है (interview में कम पूछा जाता है)।

दो Phase की प्रक्रिया

  • Memory Creation Phase (या Creation Phase): इसमें सभी variables और functions की जगह memory allocate होती है, values undefined होती हैं।
  • Execution Phase: इसमें variables को actual value assign होती है और code execute होता है।
  • हर function call पर इसी process से नया execution context बनता है।

Function Call और Execution Context

  • Function call होने पर नया sandbox (execution context) बनता है जिसमें अलग variables और execution thread होता है।
  • Function के अंदर मिले arguments और variables memory phase में undefined रहते हैं, execution phase में values assign होती हैं।
  • Function execution के बाद उसकी value वापिस global execution context में return हो जाती है।

Call Stack का कार्य

  • Call stack एक data structure है जिसमें execution context stack की तरह add/remove होते हैं (LIFO: Last In First Out)।
  • सबसे पहले global execution context आता है, फिर functions जैसे-जैसे call होते हैं, stack में push होते हैं।
  • Function की execution खत्म होते ही वे stack से remove हो जाते हैं।
  • Nested function calls में सबसे लेटेस्ट call सबसे ऊपर रहता है, और सबसे पहले remove होता है।

Practical Example (Browser Console)

  • Browser console में call stack को live देखा जा सकता है (debugger breakpoints लगाकर)।
  • Function call order, values assign और remove होने की प्रक्रिया visibly ट्रैक की जा सकती है।

Key Terms & Definitions

  • Execution Context — वह वातावरण जिसमें JS कोड रन और एक्सिक्यूट होता है।
  • Global Execution Context — सबसे पहला और main execution context जो पूरी फाइल को संभालता है।
  • Functional Execution Context — हर function call पर बनता नया execution context।
  • Memory Creation Phase — Variables/functions के लिए memory allocation phase, values undefined।
  • Execution Phase — Actual value assign और कोड का execution।
  • Call Stack — Data structure जिसमें सारे execution context स्टैक की तरह add/remove होते हैं (LIFO principle)।

Action Items / Next Steps

  • Browser console में code run कर call stack को practically observe करें।
  • Execution context और उसके phases को diagram या notes में summarize करें।
  • Functions के nested calls का खुद से code लिखकर अभ्यास करें।