🧠 Day 29 — Execution Context & Call Stack in JavaScript (Simplified) Ever wondered how JavaScript actually runs your code behind the scenes? 🤔 It all starts with Execution Context and the Call Stack 🚀 --- 🔍 What is Execution Context? 👉 It’s the environment where JavaScript code is executed There are mainly 2 types: 1. Global Execution Context → Created first 2. Function Execution Context → Created whenever a function is called --- 📌 Example function one() { console.log("One"); two(); } function two() { console.log("Two"); } one(); --- 🧠 What happens? 👉 JS creates Global Execution Context 👉 one() is pushed to Call Stack 👉 Inside one(), two() is pushed 👉 After execution, functions are popped out --- ⚡ Call Stack = LIFO 👉 Last In, First Out Global() ↓ one() ↓ two() Then: two() → removed one() → removed Global() stays --- 🚀 Why it matters ✔ Helps debug errors ✔ Understand recursion better ✔ Explains stack overflow issues --- 💡 One-line takeaway: 👉 “Execution Context creates the environment, Call Stack manages the order.” --- Once you understand this, debugging JavaScript becomes much easier. #JavaScript #ExecutionContext #CallStack #WebDevelopment #Frontend #100DaysOfCode 🚀
Execution Context & Call Stack in JavaScript Explained
More Relevant Posts
-
What will happen if you call a variable before initialization? 🤔 That is called Hoisting 👉 "JavaScript moves declarations to the top of their scope before execution" Sounds confusing? Let’s break it down 👇 When you create variables or functions, JavaScript runs your code in 2 phases: 1️⃣ Memory Creation Phase Before execution, JavaScript scans your code and allocates memory Example (mentally): var a → undefined let b → uninitialized (Temporal Dead Zone) 2️⃣ Execution Phase Now JavaScript runs your code line by line 👉 If you access variables before initialization: var → returns undefined let / const → ReferenceError Why does this happen? Because: var is initialized with undefined in memory let and const are hoisted but stay in the Temporal Dead Zone (TDZ) until the line where they are declared Simple way to remember: var => “exist, but don’t have a value yet” let / const => “Don’t touch before declaration” ⚡ Bonus: Function declarations are fully hoisted, so you can call them before defining them Curious how functions behave in hoisting? 🤔 Go Google function vs function expression in JavaScript — it’ll surprise you 👀 That’s hoisting in JavaScript 🚀 #javascript #webdevelopment #coding #frontend #learninpublic #hoisting
To view or add a comment, sign in
-
-
🚀Day 30 — Scope Chain & Scope Types in JavaScript (Simplified) Understanding scope is one of the most important fundamentals in JavaScript 🚀 --- 🔍 What is Scope? 👉 Scope decides where variables can be accessed in your code In simple words: 👉 “Who can access what?” --- ⚡ Types of Scope 1. Global Scope 👉 Variables declared outside functions or blocks let name = "John"; function greet() { console.log(name); // accessible } --- 2. Function Scope 👉 Variables declared inside a function function test() { let age = 25; console.log(age); } console.log(age); // ❌ Error --- 3. Block Scope 👉 Variables declared with let and const inside {} if (true) { let city = "Delhi"; } console.log(city); // ❌ Error --- 🔗 What is Scope Chain? 👉 If JS can’t find a variable in current scope, it looks in the outer scope, then outer again… until global scope. This is called the Scope Chain --- 🚀 Why it matters ✔ Prevents variable conflicts ✔ Helps understand closures ✔ Improves debugging skills --- 💡 One-line takeaway: 👉 “JavaScript looks upward to find variables — that’s the scope chain.” --- Mastering scope makes closures, hoisting, and debugging much easier. #JavaScript #Scope #ScopeChain #WebDevelopment #Frontend #100DaysOfCode 🚀
To view or add a comment, sign in
-
⚡ Day 7 — JavaScript Event Loop (Explained Simply) Ever wondered how JavaScript handles async tasks while being single-threaded? 🤔 That’s where the Event Loop comes in. --- 🧠 What is the Event Loop? 👉 The Event Loop manages execution of code, async tasks, and callbacks. --- 🔄 How it works: 1. Call Stack → Executes synchronous code 2. Web APIs → Handle async tasks (setTimeout, fetch, etc.) 3. Callback Queue / Microtask Queue → Stores callbacks 4. Event Loop → Moves tasks to the stack when it’s empty --- 🔍 Example: console.log("Start"); setTimeout(() => { console.log("Timeout"); }, 0); Promise.resolve().then(() => { console.log("Promise"); }); console.log("End"); --- 📌 Output: Start End Promise Timeout --- 🧠 Why? 👉 Microtasks (Promises) run before macrotasks (setTimeout) --- 🔥 One-line takeaway: 👉 “Event Loop decides what runs next in async JavaScript.” --- If you're learning async JS, understanding this will change how you debug forever. #JavaScript #EventLoop #WebDevelopment #Frontend #100DaysOfCode 🚀
To view or add a comment, sign in
-
Most people don’t understand the JavaScript Event Loop. So let me explain it in the simplest way possible: JavaScript is single-threaded. It can only do ONE thing at a time. It uses something called a call stack → basically a queue of things to execute. Now here’s where it gets interesting: When async code appears (like promises or setTimeout), JavaScript does NOT execute it right away. It sends it away to the Event Loop and then keeps running what’s in the call stack. Only when the call stack is EMPTY… the Event Loop starts pushing async tasks back to be executed. Now look at the code in the image. What do you think runs first? Actual output: A D C B Why? Because not all async is equal: Promises (microtasks) → HIGH priority setTimeout (macrotasks) → LOW priority So the Event Loop basically says: “Call stack is empty? cool… let me run all promises first… then I handle setTimeout” If you get this, async JavaScript stops feeling random. #javascript #webdevelopment #frontend #reactjs #softwareengineering
To view or add a comment, sign in
-
-
🚀 How JavaScript Works Behind the Scenes We use JavaScript every day… But have you ever thought about what actually happens when your code runs? 🤔 Let’s understand it in a simple way 👇 --- 💡 Step 1: JavaScript needs an Engine JavaScript doesn’t run on its own. It runs inside a JavaScript engine like V8 (Chrome / Node.js). 👉 Engine reads → understands → executes your code --- 💡 Step 2: Two Important Things When your code runs, JavaScript uses: 👉 Memory Heap → stores variables & functions 👉 Call Stack → executes code line by line --- 💡 Step 3: What happens internally? let name = "Aman"; function greet() { console.log("Hello " + name); } greet(); Behind the scenes: - "name" stored in Memory Heap - "greet()" stored in Memory Heap - function call goes to Call Stack - executes → removed from stack --- 💡 Step 4: Single Threaded Meaning JavaScript can do only one task at a time 👉 One Call Stack 👉 One execution at a time --- ❓ But then… how does async work? (setTimeout, API calls, promises?) 👉 That’s handled by the runtime (browser / Node.js) More on this in next post 👀 --- 💡 Why this matters? Because this is the base of: - Call Stack - Execution Context - Closures - Async JS --- 👨💻 Starting a series to revisit JavaScript from basics → advanced with focus on real understanding Follow along if you want to master JS 🚀 #JavaScript #JavaScriptFoundation #WebDevelopment #FrontendDevelopment #Coding #SoftwareEngineer #Tech
To view or add a comment, sign in
-
-
🧠 JavaScript Execution Context Explained Simply Ever wondered what actually happens when JavaScript runs your code? Behind the scenes, everything runs inside something called an Execution Context. Here’s a simple way to understand it 👇 🔹 What is Execution Context? It’s the environment where JavaScript code is evaluated and executed. There are mainly two types: • Global Execution Context • Function Execution Context 🔹 How JavaScript runs code Every execution context has 2 phases: 1️⃣ Creation Phase • Variables are set up • Functions are stored in memory • this is determined 2️⃣ Execution Phase • Code runs line by line • Values are assigned • Functions are executed 🔹 Call Stack JavaScript uses a call stack to manage execution. • When a function is called → it’s pushed to the stack • When it finishes → it’s popped out This is why JavaScript is single-threaded and synchronous by default. 🔹 Why this matters Understanding execution context helps you: ✅ understand hoisting better ✅ debug issues more effectively ✅ write predictable code 💡 One thing I’ve learned: When you understand how JavaScript runs internally, many “confusing” behaviors start making sense. Curious to hear from other developers 👇 Which JavaScript concept helped you the most in improving your fundamentals? #javascript #frontenddevelopment #webdevelopment #reactjs #softwareengineering #developers
To view or add a comment, sign in
-
-
JavaScript Hoisting 🎭 At first, it feels like variables and functions magically “float” to the top of your script. But in reality, nothing is physically moving. I like to explain it using Reserved Seats. Imagine walking into a cinema. Before the first person enters, some seats are already reserved. But the people (the values) aren’t sitting there yet. That’s exactly how the JavaScript engine works during the Creation Phase: • It scans your code • It allocates memory for declarations • It reserves space before executing a single line Code in Action: console.log(movie); var movie = "Inception"; // Output: undefined Why? Because var movie reserved the seat during creation, but the value "Inception" arrived only when execution reached that line. What about let and const? They are hoisted too but they stay inside the Temporal Dead Zone (TDZ) until their declaration line is reached. Think of it like a reserved seat that stays locked until the owner arrives with the key. Try to use it too early, and JavaScript throws an error. Why This Matters in Real Projects • Debugging unexpected undefined values • Writing predictable code • Avoiding scope confusion • Understanding how JavaScript executes behind the scenes Hoisting isn’t magic — it’s JavaScript preparing the room before the party starts. Once you understand the Creation Phase, many “weird” behaviors finally make sense. #JavaScript #ReactJS #NodeJS #FrontendEngineer #OpentoWork #SoftwareDevelopment #FullStack #JavascriptDeveloper #ReactjsDeveloper #FrontEndDeveloper #FullstackDeveloper
To view or add a comment, sign in
-
I got tired of print-debugging. So I built something different. CodeReplay🔂 lets you rewind your JavaScript execution like a video — scrub through every variable change, function call, and return value step by step. No breakpoints. No console.log spam. Just a timeline you can drag. No install. No signup. Open the link and paste your code. Here's what's under the hood: ▸ Custom Babel AST plugin that instruments your code before it runs ▸ Sandboxed iframe execution — untrusted code runs in complete isolation ▸ Immutable timeline engine — scrubbing reconstructs state via left fold ▸ Monaco Editor (VS Code's engine) with real-time line highlighting ▸ Claude AI explains each step in plain English ▸ 25 unit tests on the core engine ▸ Fully client-side The hardest part wasn't the UI. It was writing a Babel plugin that walks an AST and injects trace hooks without breaking the code. That's the kind of problem I enjoy. 🔗 Try it live: https://lnkd.in/gakdz3Up 🔗 GitHub: https://lnkd.in/gM-VmJq4 Built with React, Vite, Babel, Monaco Editor, and Claude API. #javascript #react #opensource #buildinpublic #frontend #webdevelopment
To view or add a comment, sign in
-
JavaScript is single-threaded… yet handles async like a pro. 🤯 If you’ve ever been confused about how setTimeout, Promises, and callbacks actually execute then the answer is the Event Loop. Here’s a crisp breakdown in 10 points 👇 1. The event loop is the mechanism that manages execution of code, handling async operations in JavaScript. 2. JavaScript runs on a single-threaded call stack (one task at a time). 3. Synchronous code is executed first, line by line, on the call stack. 4. Async tasks (e.g., setTimeout, promises, I/O) are handled by Web APIs / Node APIs. 5. Once completed, callbacks move to queues (macro-task queue or micro-task queue). 6. Micro-task queue (e.g., promises) has higher priority than macro-task queue. 7. The event loop constantly checks: Is the call stack empty? 8. If empty, it pushes tasks from the micro-task queue first, then macro-task queue. 9. This cycle repeats continuously, enabling non-blocking behavior. 10. Result: JavaScript achieves asynchronous execution despite being single-threaded. 💡 Master this, and debugging async JS becomes 10x easier. #JavaScript #WebDevelopment #Frontend #NodeJS #EventLoop #AsyncProgramming #CodingInterview
To view or add a comment, sign in
-
💡 JavaScript Deep Dive: Execution Context, Call Stack & Event Loop (in simple terms) If you’ve ever wondered how JavaScript actually runs your code, these 3 concepts are the backbone: 🔹 Execution Context Every time a function runs, JavaScript creates a small “workspace” for it 🧠 Inside this workspace, it keeps: The variables it needs The functions it can use And information about where to look for things (its scope) 🔹 Call Stack This is where execution contexts are stacked (LIFO – Last In, First Out). When a function is called → pushed onto the stack When it finishes → popped off 👉 It keeps track of “where we are” in the code. 🔹 Event Loop Handles async behavior (callbacks, promises, timers). Moves tasks from the callback queue to the call stack Ensures non-blocking execution 👉 This is why JavaScript can handle async operations smoothly despite being single-threaded. #JavaScript #WebDevelopment #AsyncProgramming #Frontend #CodingConcepts
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