💡 How JavaScript Code Executes (Behind the Scenes) Everything in JavaScript happens inside an Execution Context. You can imagine an execution context as a box with two parts: 1) Memory Component (Variable Environment) - Stores variables and functions as key-value pairs - Variables are stored as undefined - Functions store the entire function code 2) Code Component (Thread of Execution) - This is where the code runs - Code is executed line by line - JavaScript is synchronous and single-threaded What happens when a JavaScript program runs? When the program starts, a Global Execution Context is created. Execution happens in two phases: Memory Creation Phase - JavaScript scans the whole program - Memory is allocated to variables and functions Code Execution Phase - JavaScript executes code line by line - Values are assigned to variables - On function call, a new execution context is created Call Stack - Manages the order of execution - Global Execution Context is pushed first - Function contexts are pushed and popped after execution Understanding execution context helps in mastering hoisting, scope, closures, and async JavaScript. #JavaScript #WebDevelopment #Frontend #React #LearningInPublic
Understanding JavaScript Execution Context
More Relevant Posts
-
📌 Concept: How JavaScript Executes Code JavaScript execution finally made sense to me when I stopped asking “what runs first?” and started asking “where does it go?” Here’s the full flow, step by step 👇 1️⃣ Call Stack (Execution starts here) – JS runs synchronous code line by line – One function at a time – If the stack is busy, nothing else runs 2️⃣ Web APIs / Background Tasks – setTimeout, fetch, DOM events – These don’t block the stack – They run outside JS 3️⃣ Queues (Where async waits) 🟡 Microtask Queue (HIGH priority) – Promise.then() – async/await 🔵 Callback / Task Queue (LOW priority) – setTimeout – setInterval 4️⃣ Event Loop (The coordinator) – Checks if Call Stack is empty – Executes ALL microtasks first – Then takes one task from callback queue Important rule: Microtasks always run before timers. That’s why this happens 👇 setTimeout(() => console.log("timer"), 0); Promise.resolve().then(() => console.log("promise")); Output: promise timer Once this clicked, async behavior stopped feeling random. The Event Loop doesn’t make JS fast. It makes JS predictable. What part of async confused you the longest?
To view or add a comment, sign in
-
-
🚀 Event Loop Deep Dive — How JavaScript Really Executes Your Code Most developers use async JavaScript every day… but very few truly understand how it actually works under the hood. JavaScript is single threaded, yet it handles: • API calls • timers • promises • user interactions So what’s the secret? 👉 The Event Loop I just published a deep-dive article where I break this down step by step: ✔ How JavaScript executes synchronous code ✔ What really happens inside the Call Stack ✔ Global Execution Context explained visually ✔ Microtasks vs Macrotasks (Promises vs setTimeout) ✔ Why execution order surprises even experienced devs No shortcuts. No magic. Just how JavaScript really works. If you’ve ever been confused by execution order or faced weird async bugs this one’s for you. 📖 Read the full article here: 🔗 https://lnkd.in/dbUCv6N5 #JavaScript #EventLoop #WebDevelopment #Frontend #SoftwareEngineering #AsyncJS #React #NodeJS
To view or add a comment, sign in
-
🚀 Day 3 Not Just Motivation — Real Concepts to Build Strong Technical Understanding (Part 3) JavaScript Concept That Finally Made Everything Clear: Global Execution Context (GEC) Most JavaScript confusion doesn’t come from complex syntax. It comes from not knowing how JavaScript starts executing your code. Before the first line runs, JavaScript does something very important. 👇 It creates the Global Execution Context (GEC). What really happens when a JS file runs? 🔹 Step 1: GEC is created This is the default execution environment for your program. 🔹 Step 2: GEC is pushed into the Call Stack Yes — the Call Stack starts with GEC. And since the Call Stack follows LIFO (Last In, First Out): GEC stays at the bottom until everything finishes. No function can execute unless GEC already exists. GEC works in two phases (this is key) 1️⃣ Memory Creation Phase (Preparation) Memory is allocated before execution var → undefined Functions → full definition stored let / const → exist but uninitialized (TDZ) 👉 This explains hoisting without any mystery. 2️⃣ Execution Phase Code runs line by line Variables get actual values Functions create their own execution contexts Each new context goes on top of the Call Stack And when a function finishes? 👉 Its execution context is popped off the stack (LIFO in action). One subtle but important detail In the global execution context: Browser → this === window Node.js → this === global Why this concept matters Once you truly understand GEC: Hoisting stops being confusing Call Stack behavior makes sense Async concepts feel more logical Debugging becomes easier 📌 Key takeaway JavaScript doesn’t jump into execution. It prepares memory, sets context, pushes GEC to the Call Stack — then starts running your code. If JavaScript ever felt unpredictable, it’s not chaos. It’s a well-defined execution model — once you see it, you can’t unsee it. #JavaScript #ExecutionContext #CallStack #React #Frontend #TechConcepts #LearningInPublic
To view or add a comment, sign in
-
🚀 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗮𝘀𝘆𝗻𝗰/𝗮𝘄𝗮𝗶𝘁: 𝘄𝗵𝘆 𝗶𝘁 𝗹𝗼𝗼𝗸𝘀 𝘀𝘆𝗻𝗰𝗵𝗿𝗼𝗻𝗼𝘂𝘀 𝗯𝘂𝘁 𝗶𝘀𝗻’𝘁 JavaScript doesn’t execute async/await synchronously; it only makes asynchronous code easier to read. Example: console.log("A"); async function test() { console.log("B"); await Promise.resolve("C"); console.log("D"); } test(); console.log("E"); Output: A B E D What actually happens: 1) Global execution starts "A" is printed 2) test() is called "B" is printed 3) await Promise.resolve("C") • The promise is already resolved, but await still pauses, 𝗮𝘄𝗮𝗶𝘁 𝗻𝗲𝘃𝗲𝗿 𝗰𝗼𝗻𝘁𝗶𝗻𝘂𝗲𝘀 𝗶𝗺𝗺𝗲𝗱𝗶𝗮𝘁𝗲𝗹𝘆 • Suspends test execution and lets the rest of the code run first • The remaining code (console.log("D")) is scheduled as a microtask 4) Global code continues "E" is printed 5) Microtask queue runs async function resumes from where it paused "D" is printed See? Nothing got blocked. That’s JavaScript for you, and async/await just keeps async code readable. Thanks to Akshay Saini 🚀 for explaining this concept in Namaste Javascript, which made async/await click for me! 👏👏 #JavaScript #AsyncAwait #EventLoop #FrontendDevelopment #WebDevelopment
To view or add a comment, sign in
-
🤔 Quick question: If JavaScript is single-threaded, who decides when async code runs? When I first heard about the Event Loop, I imagined something very complex. Turns out… it’s just a coordinator that decides when JavaScript can execute async callbacks 👇 -------------------------------- console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); Promise.resolve().then(() => { console.log("Promise"); }); console.log("End"); -------------------------------- Output: - Start - End - Promise - Timeout 💡 High-level mental model: - JavaScript executes synchronous code using the Call Stack - Async tasks are handled by the runtime environment - When the stack is empty, the Event Loop checks: - Microtasks (Promises) - Then macro tasks (setTimeout, events) - It pushes the next callback onto the stack for execution Takeaway: The event loop is what makes asynchronous javascript possible. It doesn’t run code in parallel — it decides when callbacks can run. 👉 Did this execution order surprise you the first time you saw it? #JavaScript #WebDevelopment #FullStack #LearningInPublic
To view or add a comment, sign in
-
Most JavaScript developers use functions every day… But very few truly understand what happens before their code runs. Let’s talk about Execution Context. Every time JavaScript runs your code, it creates something called an execution context. There are two main types: 1️⃣ Global Execution Context 2️⃣ Function Execution Context When your file starts running, JavaScript creates the Global Execution Context. Example: var name = "Sadiq"; function greet() { var message = "Hello"; console.log(message + " " + name); } greet(); Before this code executes: Memory is created. Variables are stored (initially as undefined if declared with var). Functions are fully stored in memory. Then execution begins line by line. When greet() is called, a new Function Execution Context is created. That’s why variables inside a function don’t interfere with global ones. Execution Context explains: Why hoisting happens Why scope works the way it does Why some variables are accessible and others aren’t Understanding this changed how I read JavaScript code. Instead of asking: “What is this line doing?” I now ask: “What context is this running in?” Big difference. Are you writing JavaScript… or do you understand how JavaScript is running your code? 👇 What JavaScript concept confused you the most when you started? #JavaScript #FrontendDevelopment #WebDevelopment #SoftwareEngineering #BuildInPublic
To view or add a comment, sign in
-
💡 JavaScript Deep Dive – Global Execution Context ❓ Ever wondered how JavaScript knows what to execute first? The answer starts with the Global Execution Context (GEC). 🔹 What is Global Execution Context? The Global Execution Context is the default environment created when a JavaScript program starts running. ✔ Created only once ✔ Represents the global scope ✔ Stores all global variables & functions 🔹 How JavaScript Creates the GEC JavaScript does NOT start executing code immediately. It first goes through two phases 👇 1️⃣ Memory Creation Phase (Hoisting) Memory is allocated to variables & functions Variables → undefined Functions → stored with full definition Example: var x = 10; function greet() { console.log("Hello"); } At this stage: x → undefined greet → function definition 2️⃣ Code Execution Phase Code runs line by line Values get assigned Functions execute when called Result: x = 10 greet() runs when invoked 🔹 What else lives in Global Execution Context? this keyword In browsers → this points to the window object Global variables become properties of window 🔹 Why JavaScript is Synchronous & Single-Threaded One command at a time One call stack One thread of execution JavaScript finishes one task before starting the next. 🔹 Key Takeaways ✔ GEC is created before execution ✔ Execution happens in two phases ✔ Variables are hoisted as undefined ✔ Functions are fully hoisted ✔ JavaScript runs sequentially 👉 Next Post: Function Execution Context & Call Stack (This is where most interview questions come from 👀) #JavaScript #FrontendDeveloper #WebDevelopment #ReactJS #SoftwareEngineering #TechCareers #LearningInPublic
To view or add a comment, sign in
-
🤔 Quick question: When JavaScript runs async code, where does everything actually go? After learning about the Call Stack and Event Loop, I realized something important: JavaScript doesn’t work alone — it collaborates with Web APIs and queues 👇 --------------------------- console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); console.log("End"); ---------------------------- Output: - Start - End - Timeout 💡 What happens behind the scenes? - console.log("Start") → pushed to the Call Stack - setTimeout → handed off to Web APIs - console.log("End") → runs immediately Once the Call Stack is empty: - Event Loop checks the Task Queue - setTimeout callback is pushed back to the stack - Callback executes How the pieces fit together Call Stack → executes JavaScript Web APIs → handle timers, DOM events, network calls Queues → hold callbacks waiting to run Event Loop → coordinates everything Takeaway JavaScript executes code using the Call Stack, offloads async work to Web APIs, and uses the Event Loop to decide when callbacks can run. #JavaScript #WebDevelopment #FullStack #LearningInPublic
To view or add a comment, sign in
-
JavaScript feels simple… until someone asks: 𝐂𝐚𝐧 𝐲𝐨𝐮 𝐞𝐱𝐩𝐥𝐚𝐢𝐧 𝐡𝐨𝐰 𝐭𝐡𝐢𝐬 𝐜𝐨𝐝𝐞 𝐫𝐮𝐧𝐬 𝐢𝐧𝐭𝐞𝐫𝐧𝐚𝐥𝐥𝐲? Initially, JavaScript felt magical to me. Hoisting. Call stack. Async. I was just memorizing rules, not understanding what was really happening. Then I learned the Execution Context + Call Stack mental model (thanks to Akshay Saini 🚀 ) — and suddenly, everything clicked. 𝐇𝐨𝐰 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐚𝐜𝐭𝐮𝐚𝐥𝐥𝐲 𝐫𝐮𝐧𝐬: JavaScript runs inside an Execution Context. It starts with the 𝐆𝐥𝐨𝐛𝐚𝐥 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐂𝐨𝐧𝐭𝐞𝐱𝐭. It’s just an environment where your code is prepared and then run. Each execution context has two phases: 𝐌𝐞𝐦𝐨𝐫𝐲 𝐩𝐡𝐚𝐬𝐞 • Memory is allocated to variables and functions • Variables get a placeholder value: undefined • Functions are stored completely 𝐄𝐱𝐞𝐜𝐮𝐭𝐢𝐨𝐧 𝐩𝐡𝐚𝐬𝐞 • Code runs line by line • Values are assigned to variables • When a function is called, a new execution context is created • When the function finishes, its context is removed 𝐖𝐡𝐨 𝐦𝐚𝐧𝐚𝐠𝐞𝐬 𝐚𝐥𝐥 𝐭𝐡𝐢𝐬? The Call Stack • Global Execution Context goes first • Each function call goes on top • When finished → it gets popped off Credits to Akshay Saini 🚀 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐃𝐞𝐞𝐩 𝐃𝐢𝐯𝐞 𝐩𝐥𝐚𝐲𝐥𝐢𝐬𝐭: https://lnkd.in/gy5ypSGf Blog: https://lnkd.in/gd8ZFDiJ #javascript #webdevelopment #interviews #engineering #learning #namastejavascript #namastejs #namastedev #akshaysaini #nodejs #systemdesign
To view or add a comment, sign in
-
-
Today I learned one of the most important core concepts in JavaScript – Execution Context. Earlier, JavaScript execution felt like “magic” to me. After understanding Execution Context, I now clearly know how JavaScript reads, stores, and executes code behind the scenes. How JavaScript creates an Execution Context before running any code The two phases: Memory Creation Phase and Code Execution Phase Difference between Global Execution Context and Function Execution Context How the Call Stack manages function execution How variables and functions are allocated memory before execution Understanding this concept made me realize that JavaScript is not just about writing code, but about understanding how the engine thinks and processes instructions. 1.Memory Creation Phase: var x = 10; function greet() {} In memory: x → undefined greet → function reference 2.Code Execution Phase Global Context #JavaScript #ExecutionContext #CallStack #WebDevelopment #FrontendDevelopment #LearningJourney
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development