JavaScriptMedium
What are Cookies?
Cookies are small pieces of data (about 4 KB each) that a server or script stores in the browser and that the browser automatically sends back with every matching HTTP request; they are mainly used for sessions, preferences and tracking.
Setting cookies
- Server:
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600. - JavaScript:
document.cookie = "theme=dark; Max-Age=31536000; Path=/; SameSite=Lax". Each assignment sets one cookie; readingdocument.cookiereturns all readable cookies as one string. - A newer async API,
cookieStore, is available in Chromium-based browsers.
Attributes that matter
HttpOnly: JavaScript cannot read it. Essential for session cookies, so an XSS attack cannot steal them.Secure: sent only over HTTPS.SameSite:Lax(default in modern browsers: sent on top-level navigations, not on cross-site sub-requests),Strict(never cross-site),None(always, requiresSecure). This is the main defence against CSRF.Max-Age/Expires: without them it is a session cookie, removed when the browser closes.Domain/Path: which requests receive it.Partitioned(CHIPS): third-party cookies kept separate per top-level site.
Cookies vs Web Storage
Cookies travel with every request (adding bytes to each one), are small, and can be HttpOnly. localStorage stays in the browser, is bigger, and is always readable by JavaScript.
Privacy changes
Browsers increasingly restrict third-party cookies used for cross-site tracking, and privacy laws (GDPR) require consent for non-essential cookies.
Code example
js
1// Minimal helpers for the cookies JavaScript is allowed to see2function setCookie(name, value, { days = 365, sameSite = "Lax", path = "/" } = {}) {3 const maxAge = days * 24 * 60 * 60;4 document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}; Max-Age=${maxAge}; Path=${path}; SameSite=${sameSite}`;5}67function getCookie(name) {8 const pair = document.cookie9 .split("; ")10 .find((row) => row.startsWith(encodeURIComponent(name) + "="));11 return pair ? decodeURIComponent(pair.split("=").slice(1).join("=")) : null;12}1314function deleteCookie(name, path = "/") {15 document.cookie = `${encodeURIComponent(name)}=; Max-Age=0; Path=${path}`;16}1718try {19 setCookie("theme", "dark");20 setCookie("lang", "en-GB");21 console.log(getCookie("theme"), getCookie("lang")); // dark en-GB22 deleteCookie("theme");23 console.log(getCookie("theme")); // null24} catch (e) {25 // cookies can be disabled by the user or blocked in sandboxed iframes (like this preview)26 console.warn("Cookies are blocked in this context:", e.name);27}2829// A secure session cookie is set by the SERVER and is invisible to JavaScript:30// Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=360031console.log(navigator.cookieEnabled ? "cookies enabled" : "cookies disabled");
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.