What are Web APIs?

Web APIs are the interfaces the browser (or another host) provides to JavaScript beyond the core language, such as the DOM, fetch, timers, storage, Canvas, geolocation, the clipboard, Web Workers and many more.

Language vs platform

ECMAScript defines the language (syntax, types, Promise, Array). Everything that touches the outside world comes from the host. The browser exposes Web APIs through window (and self in workers); Node.js exposes its own APIs (and now implements many web standards like fetch, URL, AbortController and Web Streams).

Well-known groups

  • Document and UI: DOM, events, IntersectionObserver, ResizeObserver, MutationObserver, View Transitions, Popover.
  • Network: fetch, WebSocket, Server-Sent Events (EventSource), WebRTC.
  • Storage: localStorage, sessionStorage, IndexedDB, Cache API, cookies, the Origin Private File System.
  • Concurrency and timing: setTimeout, requestAnimationFrame, requestIdleCallback, Web Workers, BroadcastChannel.
  • Device and media: Geolocation, Clipboard, Notifications, MediaDevices (camera and microphone), Web Audio, Vibration.
  • Graphics: Canvas, WebGL, WebGPU.
  • Offline and installability: Service Workers, Web App Manifest, Push.

How they work with the event loop

Many Web APIs run outside the JavaScript thread (network, timers, workers) and deliver results back through the task or microtask queue.

Feature detection and permissions

Check support before use (if ("geolocation" in navigator)), and expect permission prompts for sensitive APIs, many of which also require HTTPS ("secure contexts").

Code example

js
1document.body.innerHTML = `<p id="status"></p><div style="height:150vh"></div><div id="footer">Footer</div>`;
2const status = document.getElementById("status");
3const report = (msg) => (status.textContent += msg + " | ");
4
5// Feature detection before using a Web API
6const features = {
7 fetch: "fetch" in window,
8 clipboard: "clipboard" in navigator,
9 geolocation: "geolocation" in navigator,
10 workers: "Worker" in window,
11 indexedDB: "indexedDB" in window,
12};
13console.log(features);
14
15// Timing API in sync with rendering
16requestAnimationFrame((time) => report(`frame at ${Math.round(time)}ms`));
17
18// IntersectionObserver: know when an element becomes visible (no scroll listeners needed)
19const observer = new IntersectionObserver(([entry]) => {
20 if (entry.isIntersecting) report("footer is visible");
21});
22observer.observe(document.getElementById("footer"));
23
24// Run heavy work off the main thread with a Web Worker
25const workerCode = "onmessage = (e) => postMessage(e.data.reduce((a, b) => a + b, 0));";
26const worker = new Worker(URL.createObjectURL(new Blob([workerCode], { type: "text/javascript" })));
27worker.onmessage = (e) => {
28 console.log("sum computed in a worker:", e.data); // 5050
29 worker.terminate();
30};
31worker.postMessage(Array.from({ length: 100 }, (_, i) => i + 1));

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app