JavaScript Web APIs — A Tour

The Browser Ships Hundreds of APIs. Here Are the Ones You'll Use.

JavaScript Web APIs — A Tour

A grand tour of the most useful browser APIs — timers, URL, clipboard, geolocation, observers, workers, and more.

6 min read Level 2/5 #web-apis#browser#platform
What you'll learn
  • Know what exists on the platform without a library
  • Pick the right API for common needs
  • Find your way around MDN for deeper reading

The browser ships an enormous standard library of “Web APIs”. You won’t memorize them all — and you don’t need to. This lesson is a map of the most commonly useful ones, so you know what’s there when you reach for a library.

Timers

setTimeout / setInterval script.js
const id = setTimeout(() => console.log("later"), 1000);
clearTimeout(id);   // cancel

const t = setInterval(() => console.log("tick"), 1000);
clearInterval(t);
▶ Preview: console

For animation, prefer requestAnimationFrame — it syncs with the display’s refresh rate.

URL Parsing

Don’t parse URLs with regex. The URL class handles it.

URL — read and modify script.js
const u = new URL("https://jsschools.com/js/strings?ref=hn#top");

console.log(u.host);       // "jsschools.com"
console.log(u.pathname);   // "/js/strings"
console.log(u.searchParams.get("ref"));  // "hn"

u.searchParams.set("page", "2");
console.log(u.toString());  // adds "&page=2"
▶ Preview: console

URLSearchParams standalone is great for building query strings.

Clipboard

Read and write the clipboard script.js
await navigator.clipboard.writeText("copied!");
const text = await navigator.clipboard.readText();
▶ Preview: console

Requires user gesture (a click or keypress) on most browsers.

Geolocation

Where am I? script.js
navigator.geolocation.getCurrentPosition(
  pos => console.log(pos.coords.latitude, pos.coords.longitude),
  err => console.log("denied:", err.message),
);
▶ Preview: console

User has to grant permission.

Observers

ObserverWatches for…
IntersectionObserverElement entering/leaving the viewport
ResizeObserverElement resizing
MutationObserverDOM changes on a node and descendants
PerformanceObserverPerformance entries (paint, navigation, etc.)

IntersectionObserver is what powers lazy-loading and reveal-on-scroll without firing on every scroll event.

const io = new IntersectionObserver(entries => {
  for (const e of entries) {
    if (e.isIntersecting) {
      e.target.classList.add("visible");
      io.unobserve(e.target);
    }
  }
});
for (const el of document.querySelectorAll(".reveal")) io.observe(el);

History and the Back Button

Update the URL without reloading script.js
history.pushState({ page: 2 }, "", "/p/2");
window.addEventListener("popstate", e => {
  console.log("user hit back/forward, state:", e.state);
});
▶ Preview: console

This is the foundation of SPA routers.

Workers — Off the Main Thread

A Worker runs JavaScript on a background thread. No DOM access, but no UI blocking either.

// main.js
const w = new Worker("/heavy.js", { type: "module" });
w.postMessage({ task: "factor", n: 1234567 });
w.onmessage = e => console.log("result:", e.data);

// heavy.js (the worker)
self.onmessage = e => {
  const result = doExpensiveWork(e.data);
  self.postMessage(result);
};

Service Workers are a related but distinct API — they sit between your page and the network, enabling offline support and caching.

Crypto

crypto.getRandomValues gives secure randomness. crypto.subtle gives hashes and encryption.

A secure random hex string script.js
const bytes = new Uint8Array(8);
crypto.getRandomValues(bytes);
const hex = [...bytes].map(b => b.toString(16).padStart(2, "0")).join("");
console.log(hex);   // 16 hex chars, cryptographically random
▶ Preview: console

More You Might Meet

APIWhat it does
WebSocketLong-lived two-way connection to a server
EventSourceServer-sent events (one-way streaming)
NotificationOS-level notification toasts
Audio / Video / MediaStreamPlay and capture media
Canvas / WebGL / WebGPU2D and 3D rendering
IndexedDBBigger, async, structured storage
FileSystem AccessRead/write local files (with permission)
Bluetooth, USB, SerialTalk to hardware

When you need any of these, MDN is the canonical reference. Most APIs follow the same shape: a constructor or a navigator.* object, an event-based or promise-based API, and permissions for sensitive things.

You’ve Reached the End

That wraps the JavaScript course. You started with console.log and finished with workers, observers, and crypto. The language is big; the platform is bigger. Build something with it.