A cookie is a small piece of text a website asks the browser to remember, and the browser sends it back every time it asks that site for something. Cookies are how a site recognizes you between visits. JavaScript can read and write them through document.cookie, which is an unusual property with some odd behavior. This lesson covers how it works, the attributes that control a cookie, and the habits that keep them safe.
A note about the examples. Your code in the Try It editor runs on this site, so the cookies it makes belong to this site. Every example here uses names that start with demo_, limits the cookie to the /jsdemo/ folder, and deletes it at the end, so it cannot interfere with anything else.
document.cookie returns all the cookies for the page as one string: each name=value pair separated by "; ". It never shows the settings, only the names and values:
document.cookie = "demo_a=1; path=/jsdemo/";
document.cookie = "demo_b=two; path=/jsdemo/";
const names = document.cookie.split("; ")
.filter(pair => pair.startsWith("demo_"))
.sort();
console.log(names);
document.cookie = "demo_a=; max-age=0; path=/jsdemo/";
document.cookie = "demo_b=; max-age=0; path=/jsdemo/";
Assign a name=value string to document.cookie. This looks like it should replace everything, but it does not: the browser adds or updates just that one cookie. It is the one place in JavaScript where assignment behaves like a method call.
document.cookie = "demo_user=Ava; path=/jsdemo/";
console.log(document.cookie.includes("demo_user=Ava"));
document.cookie = "demo_user=Ben; path=/jsdemo/";
console.log(document.cookie.includes("demo_user=Ava"));
console.log(document.cookie.includes("demo_user=Ben"));
document.cookie = "demo_user=; max-age=0; path=/jsdemo/";
Settings follow the value, each separated by a semicolon:
| Setting | Does |
|---|---|
max-age=86400 | keep it for this many seconds (86400 is one day) |
expires=DATE | keep it until this date, written like "Fri, 02 Oct 2026 12:00:00 GMT" |
path=/ | which part of the site gets it. / is the whole site, /jsdemo/ only that folder |
domain=example.com | share it with subdomains too (default: just this exact host) |
secure | send it only over https |
samesite=lax | whether other sites may trigger it being sent: strict, lax (usual default), or none |
Without max-age or expires a cookie is a session cookie: it disappears when the browser closes. The path matters more than it looks: a cookie set on one path is not visible on pages outside it, which is the usual cause of "my cookie vanished".
HttpOnly, which hides it from document.cookie entirely. That is what login cookies use, so that a script on the page cannot steal them. It also means you will not see every cookie that your browser holds for a site in document.cookie.
There is no delete function. You overwrite the cookie with one that has already expired, using the same name and the same path. Setting max-age=0 does it:
document.cookie = "demo_user=; max-age=0; path=/jsdemo/";
If the path does not match the original, the browser treats it as a different cookie and the original stays.
A cookie value cannot contain semicolons, commas, or spaces without causing trouble. Wrap it in encodeURIComponent when you write and decodeURIComponent when you read:
const value = "a b;c";
console.log(encodeURIComponent(value));
console.log(decodeURIComponent("a%20b%3Bc"));
Working with the cookie string by hand is clumsy, so most code wraps it in three small functions:
function setCookie(name, value, days) {
const seconds = days * 24 * 60 * 60;
document.cookie = name + "=" + encodeURIComponent(value) +
"; max-age=" + seconds + "; path=/jsdemo/";
}
function getCookie(name) {
const pair = document.cookie.split("; ").find(row => row.startsWith(name + "="));
return pair ? decodeURIComponent(pair.split("=")[1]) : null;
}
function deleteCookie(name) {
document.cookie = name + "=; max-age=0; path=/jsdemo/";
}
setCookie("demo_theme", "dark & light", 7);
console.log(getCookie("demo_theme"));
console.log(getCookie("demo_nothing"));
deleteCookie("demo_theme");
console.log(getCookie("demo_theme"));
What does this line do to the other cookies already on the page?
document.cookie = "theme=dark";
document.cookie adds or updates only the cookie named theme. All the others are untouched.How do you delete a cookie from JavaScript?
path) using an expired lifetime, such as max-age=0. There is no delete function.What does this log?
document.cookie = "demo_x=1; path=/jsdemo/";
console.log(typeof document.cookie);
document.cookie = "demo_x=; max-age=0; path=/jsdemo/";
string. document.cookie always reads back as one string of name=value pairs, however many cookies there are.Ava & Ben in a cookie with the helpers and read it backgetCookie returns null