async vs defer: Understanding Browser Parsing

One Frontend Interview Question That Never Goes Away: async vs defer This is one of the most commonly asked frontend interview questions — and many candidates answer it without understanding why it exists. Before talking about async and defer, you must understand how the browser parses HTML. 🧠 How HTML Parsing Actually Works When the browser loads a page, it parses HTML from top to bottom. If it encounters a <script> tag without async or defer: 1. HTML parsing stops 2. The JavaScript file is downloaded 3. The script is executed 4. HTML parsing resumes ⚠️ This is blocking behavior, and it can significantly slow down page load and delay rendering. 🚀 What async Does With async: Script downloads in parallel while HTML continues parsing Script executes as soon as it finishes downloading HTML parsing pauses only during execution Execution order is NOT guaranteed ✅ Best suited for: Scripts that are independent Scripts that don’t rely on DOM or other scripts 📌 Common examples: Analytics Ads Tracking scripts 🕒 What defer Does With defer: Script downloads in parallel with HTML parsing Execution is delayed until HTML parsing completes Scripts execute in the same order they appear in the document ✅ Best suited for: Scripts that depend on the DOM Scripts that rely on other scripts 🧠 Easy Rule to Remember Independent + performance-focused → async DOM-dependent + ordered execution → defer Understanding this small detail shows interviewers that you know how browsers work — not just JavaScript syntax. And yes, it directly impacts page performance and frontend architecture. 👉 Follow Rahul R Jain for more real interview insights, React fundamentals, and practical frontend engineering content. #JavaScript #FrontendDevelopment #WebPerformance #BrowserInternals #WebDev #FrontendInterview #LearningInPublic

To view or add a comment, sign in

Explore content categories