Day 2: How JavaScript actually runs! (Execution Context Explained) 🚀 Yesterday, we covered the basics. Today, let’s look under the hood. If you’ve ever wondered why your code behaves a certain way, the answer lies in the Execution Context. Think of JavaScript as a two-step process. It doesn't just "run" code; it prepares first. 1. Phase 1: Memory Creation (The Setup) Before executing a single line, JS scans your code. It "skips" the values and just allocates space. Variables are set to undefined. Functions are stored entirely. This is why you can call a function before it's even defined! (Hoisting). 2. Phase 2: Code Execution (The Action) ⚡ Now, JS runs the code line-by-line. It fills those empty memory slots with real values and executes functions. 🖼️ Let’s break down the image example: Look at the code in the attached image: Memory Phase: x and y are born as undefined. The double function is saved. Execution Phase: x becomes 10. The Call Stack: When double(10) is called, a new "mini-context" is pushed to the top of the stack. It calculates 10 * 2, returns 20, and then pops off the stack to keep things clean. The Key Rule: JS is Single-Threaded. It can only do one thing at a time. The Call Stack ensures it never gets confused about what to do next. Understanding this is the "bridge" between being a beginner and a pro. Once you get the Execution Context . #JavaScript #WebDevelopment #100DaysOfCode #ProgrammingTips #SoftwareEngineering #CodingJourney #BeginnerCoder
JavaScript Execution Context Explained
More Relevant Posts
-
🚀 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
-
🚀 How JavaScript Executes Your Code (Simply Explained) Ever wondered what really happens when JavaScript runs your code? Here’s a clean mental model that helped me connect Execution Context, Hoisting, Closures, and this 👇 🧠 JavaScript uses Execution Contexts to run code When a JS program starts: 1️⃣ Global Execution Context (GEC) is created Global memory is allocated this is initialized (window in browser) 2️⃣ Two phases happen in every execution context: 🔹 Memory Creation Phase var → undefined let / const → hoisted but uninitialized (TDZ) Function declarations → fully stored 👉 This explains Hoisting 🔹 Execution Phase Values are assigned Code runs line by line 3️⃣ Function calls create new Function Execution Contexts Each function gets its own execution environment Managed using the Call Stack (LIFO) 🔗 How concepts connect: Hoisting → Happens due to memory creation phase Closures → Inner functions remember their lexical execution context even after the outer function finishes this keyword → Decided when execution context is created Arrow functions → Don’t have their own this; they inherit it from lexical scope ✨ Key takeaway Execution Context is the foundation. Hoisting, Closures, and this are just outcomes of how it works. Currently diving deep into JavaScript fundamentals and loving how everything connects 🔥 Would love to hear how you explain this or if I missed anything! #JavaScript #WebDevelopment #Frontend #LearningInPublic #ExecutionContext #Closures #Hoisting #ThisKeyword #MERN
To view or add a comment, sign in
-
-
JavaScript – Execution Context Before JavaScript runs even a single line of your code, it prepares a working space for it. That space is called an Execution Context. In simple words: Execution Context is where JavaScript remembers things and runs your code Whenever JavaScript runs: ● Your whole program → one big Execution Context ● Every function call → a new small Execution Context Each one is created in two steps: 1️⃣ Memory Phase ● Variables are created → undefined ● Functions are stored fully 2️⃣ Execution Phase ● Code runs line by line ● Variables get real values ● Functions are executed Example: *** var a = 5; function show() { var b = 3; console.log(a + b); } show(); *** How JavaScript works: ● Creates a global context ◦ a → undefined ◦ show → saved ● Runs code ◦ a = 5 ◦ show() is called → new context is created ● Inside show() ◦ b = 3 ◦ Prints 8 JavaScript manages these contexts using a Call Stack: ● Global goes first ● Each function goes on top ● When a function finishes, it is removed This is why understanding Execution Context helps you: ● Understand hoisting ● Read call stack errors ● Master scope & closures ● Debug with confidence This is how JavaScript thinks before it acts. #Day1 #JavaScript #Frontend #WebDevelopment #LearningInPublic #React #Developers #CareerGrowth
To view or add a comment, sign in
-
Ever wondered what actually happens behind the scenes when JavaScript runs your code? 🤔 How Variables & Functions Work Behind the Scenes in JavaScript Today I focused on understanding what actually happens inside the JavaScript engine when we write variables and functions — and it completely changed how I read JS code. 🧠 Step 1: JavaScript Creates an Execution Context Before executing any code, JavaScript creates an Execution Context. This happens in two phases: 1. Creation Phase (Memory Allocation Phase) In this phase, JavaScript scans the entire code before running it and prepares memory. ✔ Variables var variables are allocated memory and initialized with undefined let and const are also allocated memory, but they stay in the Temporal Dead Zone (TDZ) until initialized ✔ Functions Function declarations are stored fully in memory (function body included) Function expressions & arrow functions behave like variables and depend on var / let / const 👉 This is the real reason hoisting exists. 2.Execution Phase (Code Runs Line by Line) Now JavaScript starts executing the code: Variables get their actual values Functions are executed when they are called A new Function Execution Context is created for every function call Each context is pushed to the Call Stack After execution, it is removed from the stack Why Functions Can Be Called Before Declaration? because function declarations are fully hoisted during the creation phase. 💡 Key Takeaways JavaScript doesn’t execute code directly — it prepares first Hoisting is a byproduct of the creation phase var, let, and const differ because of how memory is allocated Understanding execution context makes debugging much easier. Mastering the basics is what truly levels up a developer. Thanks to Anshu Pandey and Sheryians Coding School #JavaScript #JavaScriptbasics #ES6 #JSEngine #coding
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
-
-
🧠 “JavaScript is Single-Threaded” What Does That ACTUALLY Mean? You’ve probably heard this line many times: JavaScript is single-threaded But what does it really mean? And why is it actually a good thing? Let’s break it down simply 👇 -- What is a “thread”? A thread means a path of execution. - Single-threaded 👉 does one task at a time - Multi-threaded 👉 can do multiple tasks at the same time -- JavaScript is single-threaded means… - JavaScript has ONE call stack - It can run ONE piece of code at a time Example: console.log("A"); console.log("B"); console.log("C"); Output will always be: A B C - No skipping. No parallel run. - Everything runs line by line. - Real-life analogy 🚶♂️ Think of one cashier at a shop: - One customer is handled at a time - Others wait in a queue - No confusion - No mixed payments 💸 That cashier = JavaScript thread - But wait… JavaScript feels fast 🤔 Yes! Because of: ✅ Event Loop ✅ Callbacks / Promises ✅ Async / Await Long tasks (API calls, timers, file reading): - Are sent outside - JavaScript continues working - Result comes back later So JavaScript is: - Single-threaded but non-blocking -- Why NOT double-threaded? Multi-threading causes: - Race conditions ❌ - Deadlocks ❌ - Complex debugging ❌ JavaScript avoids this by: - Keeping execution simple - Making code predictable - Reducing bugs Simple > Complex -- One-line summary 💡 JavaScript runs one thing at a time, but smartly handles waiting tasks in the background If this made “single-threaded” clear, give it a 👍 Many developers fear this term but it’s actually JavaScript’s strength 💛 #JavaScript #Single_Threaded #Beginners #QA #Learning
To view or add a comment, sign in
-
-
So JavaScript has this thing called hoisting. It's like a behind-the-scenes magic trick. Variables and functions get moved to the top of their scope. This happens way before the code actually runs, during the memory creation phase. Only the declarations get hoisted, not the actual values. Think of it like setting up a stage - you're just putting the props in place, but not actually using them yet. JavaScript runs in two phases: memory creation and code execution. During the memory creation phase, variables declared with var are like empty boxes - they're initialized with undefined. Function declarations, on the other hand, are like fully formed actors - they're stored in memory, ready to go. This means you can access variables before you've even assigned a value to them. It's like trying to open an empty box - you'll just get undefined. But if you try to access a variable that doesn't exist at all, JavaScript will throw a ReferenceError - it's like trying to open a box that's not even there. Here's the thing: function declarations are fully hoisted, so you can call them before they're even declared. But function expressions are like variables - they're hoisted as undefined, so trying to call them as a function will throw a TypeError. For example, consider this code: var x = 7; function getName() {...}. You can call getName() before it's declared, and it'll work just fine. But if you try to access x before it's assigned a value, you'll just get undefined. It's all about understanding how hoisting works in JavaScript. Check out this article for more info: https://lnkd.in/g_Jh4Vd8 #JavaScript #Hoisting #Coding
To view or add a comment, sign in
-
Ever wondered how JavaScript handles asynchronous code while being single-threaded? Here’s a visual breakdown using setTimeout() 👇 🔄 Execution Flow Explained 1️⃣ Global Execution (Main Function) When the program starts, the main function (global execution context) is pushed into the Call Stack. All synchronous code runs first. 2️⃣ Encountering an Async Function When JavaScript encounters an asynchronous function like setTimeout, it: Pushes it into the Call Stack Immediately removes (pops) it after registering the task 3️⃣ Web API Takes Control The async task is handed over to the Web API, where: The timer starts running (e.g., 4 seconds) JavaScript does NOT wait — it continues executing other code 4️⃣ Callback Queue Once the timer completes: The callback function (console.log("Hello World")) is pushed into the Callback Queue 5️⃣ Event Loop in Action The Event Loop continuously checks: Is the Call Stack empty? 6️⃣ Execution When the Call Stack becomes empty: The Event Loop moves the callback from the Callback Queue to the Call Stack The callback executes and prints the output 🎉 JavaScript is single-threaded, but thanks to: Web APIs Callback Queue Event Loop …it can handle non-blocking asynchronous operations efficiently. If you have any doubts or want to discuss this further, feel free to connect with me or drop a comment. Happy to help! Apoorv M. #JavaScript #EventLoop #WebAPI #AsynchronousJavaScript #Frontend #WebDevelopment #Programming #LearnJavaScript
To view or add a comment, sign in
-
-
⚙️ JavaScript Under the Hood: Execution Context & Call Stack (Simply Explained) Every line of JavaScript you write runs inside something called an Execution Context. Understanding this changes how you debug, optimize, and reason about your code. 🧠 What is an Execution Context? An execution context is the environment where JavaScript: • Creates variables and functions • Determines the value of this • Executes code line by line There are three types: 1️⃣ Global Execution Context – created first (for your entire file) 2️⃣ Function Execution Context – created for every function call 3️⃣ Eval Execution Context – rarely used 📦 What happens inside an Execution Context? Each context has two phases: 1. Creation Phase • Memory is allocated • Variables are set to undefined • Functions are fully hoisted • this is assigned 2. Execution Phase • Code runs line by line • Variables get real values • Functions execute 🪜 How the Call Stack Works The Call Stack manages execution order using LIFO (Last In, First Out): • Global Context → pushed first • Function call → new context pushed • Function finishes → context popped This is why: ✔ Nested functions work ✔ Recursion works ✔ Stack overflow happens ⚡ Why This Matters in Real Projects Understanding this helps you: • Debug scope issues & hoisting bugs • Fix recursion and infinite call errors • Reason about performance • Master async behavior later (event loop, microtasks) 🚀 Final Thought Frameworks come and go. But Execution Context + Call Stack is pure JavaScript DNA. If you truly understand this, you write more predictable, performant, and debuggable code. #JavaScript #Frontend #WebDevelopment #CallStack #ExecutionContext #DeveloperLife #EngineeringBasics
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
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