Cookies (document.cookie) in JavaScript
Cookies are small server-readable client storage: document.cookie reads all visible cookies as one semicolon-joined string (parse it yourself), while assignment (document.cookie = "k=v; Max-Age=3600; Path=/") sets with attributes — Expires/Max-Age lifetime, Path/Domain scope, Secure (HTTPS-only), SameSite (CSRF policy).
HttpOnly cookies stay invisible to JavaScript entirely (XSS-proof session tokens — set them server-side), and ~4KB per-cookie limits plus per-request overhead make cookies wrong for large data (use storage/IndexedDB). Modern defaults (Lax SameSite) secure the common cases automatically.
Read all, write one
document.cookie returns every visible cookie joined — assignment sets exactly one cookie per statement.
Attributes control scope
Max-Age/Expires lifetime, Path/Domain visibility, Secure transport, SameSite cross-site policy.
HttpOnly hides from JS
Server-set HttpOnly session cookies never reach document.cookie — XSS cannot steal what JS cannot read.
Small and server-bound
~4KB each, sent on every request — session IDs and prefs, never bulk data or secrets in JS-reachable form.
document.cookie = "theme=dark; Max-Age=31536000; Path=/";console.log(document.cookie.includes("theme=dark"));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.