One of the most common JavaScript interview questions: "Why does setTimeout with 0ms delay not run immediately?" Most developers cannot answer this correctly. Here is the full explanation: JavaScript is single-threaded. It can only do one thing at a time. The Event Loop is how it manages everything else. The execution order is always the same: 1 — Synchronous code runs first All regular code on the Call Stack executes immediately. 2 — Microtasks run second Promises, async/await — these run before anything else once the Call Stack is empty. 3 — Macrotasks run last setTimeout, setInterval, DOM events — these wait until ALL microtasks are done. This is why setTimeout with 0ms still runs after a Promise. The Promise is a microtask. setTimeout is a macrotask. Microtasks always win. Understanding this prevents real bugs in production — async state updates, race conditions, unexpected render order. Save this post for your next async debugging session. Have you ever been confused by JavaScript async order? Drop a comment below. #JavaScript #WebDevelopment #Frontend #SoftwareEngineering #AsyncJavaScript
Damanpreet Kaur’s Post
More Relevant Posts
-
15 JavaScript interview questions. DOM Manipulation & Events. Answer karo comments mein 👇 DOM Basics Q1. What is the DOM? Q2. What is the difference between querySelector and getElementById? Q3. What is the difference between textContent and innerHTML? Events Q4. What is an event listener and how do you add one? Q5. How do you remove an event listener? Q6. What is event bubbling? Q7. What is the difference between event bubbling and event capturing? Q8. What is e.stopPropagation() and when do you use it? Q9. What is e.preventDefault() and when do you use it? Q10. What is the difference between e.target and e.currentTarget? Event Delegation Q11. What is event delegation and why is it important? Q12. How does React use event delegation internally? Advanced Q13. What are synthetic events in React? Q14. Why should you avoid direct DOM manipulation in React? Q15. What is the difference between mouseenter and mouseover? Full answers + code on GitHub 👇 https://lnkd.in/dj72-XEi #JavaScript #JStoReact #InterviewPrep #WebDevelopment #Frontend #ReactJS #30DayChallenge #JavaScriptTips #FrontendDeveloper #100DaysOfCode
To view or add a comment, sign in
-
Most developers use JavaScript. Only a few actually understand it deeply. That’s the difference between getting rejected and getting selected in frontend interviews. If you want to stand out, these JavaScript concepts are non-negotiable 👇 🧠 Execution Context, Call Stack & Hoisting 🔒 Closures, Scope & Lexical Environment ⚡ Async JavaScript (Promises, Async/Await, Fetch) 🔁 Event Loop, Microtasks & Callback Queue 🧩 Objects, Prototypes & this keyword 🛠️ Call, Apply, Bind 📦 Arrays (map, filter, reduce) 🚀 ES6+ (Destructuring, Spread, Modules) 🌐 DOM Manipulation & Event Delegation 🔥 Debouncing & Throttling 🧪 Error Handling & Memory Management You need strong JavaScript fundamentals. Master this → You’re already ahead of most developers. #javascript #frontenddeveloper #webdevelopment #reactjs #coding #developers #100DaysOfCode
To view or add a comment, sign in
-
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗘𝘃𝗲𝗻𝘁𝗟𝗼𝗼𝗽 𝗘𝘅𝗽𝗹𝗮𝗶𝗻𝗲𝗱 (𝗔 𝗠𝘂𝘀𝘁‐𝗞𝗻𝗼𝘄 𝗖𝗼𝗻𝗰𝗲𝗽𝘁) Understanding the JavaScript Event Loop is a game changer for writing efficient and predictable asynchronous code. Many developers use setTimeout and Promises every day — but far fewer truly understand how JavaScript executes async tasks behind the scenes. Let’s break it down 👇 𝗛𝗼𝘄 𝘁𝗵𝗲 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 𝗪𝗼𝗿𝗸𝘀 • JavaScript runs on a single thread • Synchronous code executes first via the Call Stack • Then Microtasks run (like Promises) • Next, one Macrotask executes (timers, events) • This cycle continues repeatedly 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗣𝗿𝗶𝗼𝗿𝗶𝘁𝘆 ➡️ Synchronous ➡️ Microtasks ➡️ Macrotasks 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗰𝗼𝗻𝗰𝗲𝗽𝘁 𝗺𝗮𝘁𝘁𝗲𝗿𝘀 ✅ Debug async issues with confidence ✅ Avoid unexpected execution order ✅ Build more predictable React applications ✅ Frequently tested in frontend interviews Credit: owner Follow Rensith Udara Gonalagoda for more related content! 🤔 Having Doubts in technical journey? 🚀 Book 1:1 session with me : https://lnkd.in/gQfXYuQm 🚀 Subscribe and stay up to date: https://lnkd.in/dGE5gxTy 🚀 Get Complete React JS Interview Q&A Here: https://lnkd.in/d5Y2ku23 🚀 Get Complete JavaScript Interview Q&A Here: https://lnkd.in/d8umA-53 #JavaScript #EventLoop #FrontendDevelopment #ReactJS #WebDevelopment #InterviewPrep #AsyncJavaScript #SoftwareEngineering
To view or add a comment, sign in
-
-
🔥 JavaScript Interview Question That Trips Many Developers Here’s a simple-looking question that reveals how well you understand this in JavaScript 👇 const obj = { name: 'Alice', greet() { console.log(this.name); }, greetArrow: () => { console.log(this.name); }, }; obj.greet(); obj.greetArrow(); const fn = obj.greet; fn(); ❓ What will be the output? ✅ Answer: Alice undefined undefined 💡 Explanation (Must-Know for Interviews): 1️⃣ obj.greet() Regular function this → refers to obj 👉 Output: Alice 2️⃣ obj.greetArrow() Arrow function Doesn’t have its own this Takes this from outer (global) scope 👉 Output: undefined 3️⃣ fn() Function is detached from object this is lost (defaults to global/undefined) 👉 Output: undefined 🧠 Key Takeaways: ✔ this depends on how a function is called ✔ Arrow functions don’t bind this ✔ Extracting methods can break this 💥 Pro Tip: If you want to preserve this: const fn = obj.greet.bind(obj); fn(); // Alice #JavaScript #Frontend #WebDevelopment #InterviewPrep #CodingInterview #JSConcepts
To view or add a comment, sign in
-
🚀 JavaScript Event Loop Explained (Must-Know Concept) Understanding the Event Loop is a game changer when it comes to writing efficient and predictable asynchronous JavaScript code. We use setTimeout, Promises, and async/await every day — but do we really know what’s happening behind the scenes? 🤔 Let’s break it down 👇 🔹 How the Event Loop Works: • JavaScript runs on a single thread • Synchronous code executes first (Call Stack) • Then Microtasks run (e.g., Promises, queueMicrotask) • Next, one Macrotask executes (e.g., setTimeout, events) • This cycle keeps repeating continuously ⚡ Execution Priority: 1️⃣ Synchronous Code 2️⃣ Microtasks 3️⃣ Macrotasks 💡 Why this matters: ✅ Helps debug async issues easily ✅ Avoids unexpected execution order ✅ Builds more predictable React apps ✅ Frequently asked in frontend interviews 📌 Pro Tip: Always remember — Promises (microtasks) run before setTimeout (macrotasks), even with 0ms delay! 💬 Are you confident with the Event Loop? Drop your thoughts below! #JavaScript #EventLoop #FrontendDevelopment #ReactJS #WebDevelopment #AsyncJavaScript #InterviewPrep #SoftwareEngineering
To view or add a comment, sign in
-
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗘𝘃𝗲𝗻𝘁𝗟𝗼𝗼𝗽 𝗘𝘅𝗽𝗹𝗮𝗶𝗻𝗲𝗱 (𝗔 𝗠𝘂𝘀𝘁‐𝗞𝗻𝗼𝘄 𝗖𝗼𝗻𝗰𝗲𝗽𝘁) Understanding the JavaScript Event Loop is a game changer for writing efficient and predictable asynchronous code. Many developers use setTimeout and Promises every day — but far fewer truly understand how JavaScript executes async tasks behind the scenes. Let’s break it down 👇 𝗛𝗼𝘄 𝘁𝗵𝗲 𝗘𝘃𝗲𝗻𝘁 𝗟𝗼𝗼𝗽 𝗪𝗼𝗿𝗸𝘀 • JavaScript runs on a single thread • Synchronous code executes first via the Call Stack • Then Microtasks run (like Promises) • Next, one Macrotask executes (timers, events) • This cycle continues repeatedly 𝗘𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗣𝗿𝗶𝗼𝗿𝗶𝘁𝘆 ➡️ Synchronous ➡️ Microtasks ➡️ Macrotasks 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗰𝗼𝗻𝗰𝗲𝗽𝘁 𝗺𝗮𝘁𝘁𝗲𝗿𝘀 ✅ Debug async issues with confidence ✅ Avoid unexpected execution order ✅ Build more predictable React applications ✅ Frequently tested in frontend interviews #JavaScript #EventLoop #FrontendDevelopment #ReactJS #WebDevelopment #InterviewPrep #AsyncJavaScript #SoftwareEngineering
To view or add a comment, sign in
-
-
15 JavaScript interview questions. Promises & Async/Await. Answer karo comments mein 👇 Promises Q1. What is a Promise in JavaScript and why does it exist? Q2. What is the difference between resolve and reject? Q3. What does .then() return? Q4. What is the difference between .catch() and the second argument of .then()? Q5. What does .finally() do? Async/Await Q6. What is async/await and how does it relate to Promises? Q7. What does an async function always return? Q8. Can you use await outside an async function? Q9. What happens if you don't use try/catch with async/await? Q10. What is the difference between sequential and parallel async calls? Promise Methods Q11. What is the difference between Promise.all and Promise.allSettled? Q12. What is Promise.race and when would you use it? React Connection Q13. Why can't useEffect callback be directly async? Q14. What is the standard loading/error/data pattern in React? Q15. What is the difference between async errors and sync errors in React? Full answers + code on GitHub 👇 https://lnkd.in/dj72-XEi #JavaScript #JStoReact #InterviewPrep #WebDevelopment #Frontend #ReactJS #30DayChallenge #JavaScriptTips #FrontendDeveloper #100DaysOfCode
To view or add a comment, sign in
-
🚀 JavaScript Interview Question: Functions Today in my mock interview, I was asked: 👉 What is a function in JavaScript? 👉 How many types of functions are there? 👉 What is the syntax? ✅ What is a Function? A function in JavaScript is a reusable block of code designed to perform a specific task. It helps avoid repetition and makes code modular and organized. 📌 Types of Functions in JavaScript Function Declaration function greet() { console.log("Hello World"); } Function Expression const greet = function() { console.log("Hello World"); }; Arrow Function (ES6) const greet = () => { console.log("Hello World"); }; IIFE (Immediately Invoked Function Expression) (function() { console.log("Hello World"); })(); 💡 Why functions are important? ✔ Code reusability ✔ Better organization ✔ Easy debugging ✔ Cleaner and scalable code 📚 I’m currently learning JavaScript and improving my frontend development skills step by step. #JavaScript #FrontendDevelopment #ReactJS #WebDevelopment #MERNStack #LearningInPublic
To view or add a comment, sign in
-
One of the interesting questions asked in my interview was: ❓ “What are Microtasks and Macrotasks in JavaScript?” 💠 Understanding the Concept 🔹 JavaScript uses an event loop to handle asynchronous operations. 📌 Tasks are mainly divided into: 👉 Micro tasks : 🔹 High priority tasks 🔹 Executed immediately after the current synchronous code 🔹 Processed before macrotasks ✅ Examples: 🔹 Promise.then() 🔹 catch, finally 🔹 queueMicrotask() 👉 Macro tasks : 🔹 Lower priority compared to microtasks 🔸 Executed after microtasks queue is empty ✅ Examples: 🔹 setTimeout() 🔹 setInterval() 🔹 setImmediate() (Node.js) 🔄 Execution Order 🔹 Run synchronous code 🔹 Execute all microtasks 🔹 Execute one macrotask 🔹 Repeat This question tests understanding of Event loop, Asynchronous behavior and Execution order in JavaScript #JavaScript #EventLoop #AsyncProgramming #InterviewPrep #WebDevelopment #Learning #frontend #interviewExxperiance #interview
To view or add a comment, sign in
-
Most developers think they know JavaScript. Until they sit in an interview. If you’re preparing for frontend roles, these are the most important JavaScript topics you can’t skip 👇 🔹 Core Concepts (Non-Negotiable) • Scope & closures • this, call, apply, bind • Hoisting (var, let, const) • Prototypal inheritance • Shallow vs deep copy 🔹 Async JavaScript (Very Important) • Event loop (microtasks vs macrotasks) • Promises & async/await • Promise.all, race, allSettled • Error handling in async code 🔹 Functions & Patterns • First-class functions • Currying • Debouncing & throttling • Memoization 🔹 Performance & Memory • Memory leaks & garbage collection • Avoiding unnecessary computations • Understanding reference vs value • Optimizing loops & operations 🔹 Modern JavaScript (ES6+) • Arrow functions • Destructuring • Spread & rest operators • Optional chaining & nullish coalescing • Modules (import/export) 💡 Most candidates don’t fail because they haven’t seen these topics. They fail because they can’t explain them clearly or apply them in real scenarios. If you can confidently explain and implement these You’re already ahead of most developers. Which JavaScript topic took you the longest to understand? 👇 #JavaScript #Frontend #WebDevelopment #CodingInterview #SoftwareEngineering
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