JavaScript Web APIs Explained

Day-22 What Are Web APIs in JavaScript? If JavaScript is single-threaded… Then how does this work? 👇 setTimeout(() => { console.log("Hello"); }, 2000); Or this? fetch("https://lnkd.in/dC3Rsk9V") .then(res => res.json()) .then(data => console.log(data)); JavaScript alone cannot do these things. 👉 These are powered by Web APIs. 🔹 What Are Web APIs? Web APIs are browser-provided features that JavaScript can use. They are NOT part of the JavaScript language itself. They are provided by: Chrome Firefox Safari Node.js (with its own APIs) 🔥 Common Web APIs ✔ setTimeout() ✔ setInterval() ✔ fetch() ✔ DOM APIs (document, querySelector) ✔ localStorage ✔ console All of these are provided by the browser environment. 🔥 How Web APIs Work with Event Loop Let’s understand flow 👇 console.log("Start"); setTimeout(() => { console.log("Timer Done"); }, 2000); console.log("End"); Execution Flow: 1️⃣ console.log("Start") → Call Stack 2️⃣ setTimeout → Goes to Web API 3️⃣ Timer runs in browser environment 4️⃣ After 2 seconds → Callback moves to Callback Queue 5️⃣ Event Loop pushes it to Call Stack (when empty) Output: Start End Timer Done 🔹 Important Understanding 👉 JavaScript Engine = Executes code 👉 Web APIs = Handle async operations 👉 Event Loop = Manages execution order Without Web APIs, async JS would not exist. 🔥 Node.js Has Its Own APIs In Node.js: setTimeout fs http process These are provided by the Node runtime — not the JS engine. Remeber for interview Question: Is setTimeout part of JavaScript? Answer: ❌ No ✅ It’s a Web API provided by the browser (or Node runtime) #JavaScript #WebAPI #EventLoop #AsyncJS #Frontend #LearnInPublic

To view or add a comment, sign in

Explore content categories