Fetch & requests
The Fetch API returns a Promise that resolves once headers arrive — you still need a second step to read the body.
fetch(url)
sends the request
Response
headers + status resolve
response.json()
reads and parses the body
Basic fetch with error handling
const response = await fetch('/api/users')
if (!response.ok) throw new Error(`Request failed: ${response.status}`)
const users = await response.json()Watch out
fetch only rejects on network failure — a 404 or 500 response still resolves. Always checkresponse.ok.Client-side storage
| API | Persists | Best for |
|---|---|---|
localStorage | Until cleared | Small key-value preferences (strings only). |
sessionStorage | Until tab closes | Transient state scoped to one tab. |
| Cookies | Until expiry | Data the server needs to read too, e.g. session tokens. |
IndexedDB | Until cleared | Larger structured data, offline-first apps. |
| Cache API | Until cleared | Caching network responses, typically via a Service Worker. |
Watch out
Never store sensitive tokens in
localStorage — it's readable by any script on the page (vulnerable to XSS). Use an httpOnly cookie for session credentials instead.Observers
| API | Watches |
|---|---|
IntersectionObserver | When an element enters/exits the viewport — infinite scroll, lazy loading, fade-ins. |
ResizeObserver | An element's size changing — responsive components without a window resize listener. |
MutationObserver | Changes to the DOM tree itself — attributes, children, text. |
PerformanceObserver | Performance entries like Core Web Vitals (LCP, CLS, INP). |
Workers
| API | Runs | Good for |
|---|---|---|
Web Worker | Background thread, tied to one tab | CPU-heavy work off the main thread (parsing, image processing). |
Service Worker | Background, shared across tabs/origin | Offline caching, push notifications, PWA install. |
Realtime & messaging
| API | Direction | Good for |
|---|---|---|
WebSocket | Bidirectional | Chat, live cursors, collaborative editing. |
EventSource (SSE) | Server → client only | Streaming updates, AI token streaming, live logs. |
BroadcastChannel | Tab ↔ tab, same origin | Syncing state across open tabs (e.g. logout everywhere). |
Device & media
| API | Does |
|---|---|
navigator.clipboard | Read/write the system clipboard (requires a user gesture). |
navigator.geolocation | Request the user's current position — always permission-gated. |
Notification | Show a native OS notification, after requesting permission. |
MediaDevices.getUserMedia | Access the camera/microphone stream. |
History & URL
| API | Does |
|---|---|
history.pushState() | Add a new entry to the browser history without a full navigation. |
history.replaceState() | Replace the current entry — no back-button entry created. |
new URL(str) | Parse a URL into its parts (origin, pathname, searchParams). |
URLSearchParams | Read and build query strings without manual string concatenation. |
