SKU // 130

JS Cookies

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.

// What a Cookie Is

// Reading Cookies

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/";
["demo_a=1", "demo_b=two"]

// Writing a Cookie

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/";
true false true

// Settings That Control a Cookie

Settings follow the value, each separated by a semicolon:

Setting Does
max-age=86400keep it for this many seconds (86400 is one day)
expires=DATEkeep 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.comshare it with subdomains too (default: just this exact host)
securesend it only over https
samesite=laxwhether 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 cannot be set from JavaScript
A server can mark a cookie 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.

// Deleting a 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.

// Special Characters: Encode the Value

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"));
a%20b%3Bc a b;c

// Helper Functions

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"));
dark & light null null

// Limits and Habits

Cookies or Web Storage?
If only your page needs to remember something (a theme, a score), Web Storage in the next section is simpler and does not send the data to the server on every request. Use cookies when the server needs to see the value, such as a login.

// Quick Check

What does this line do to the other cookies already on the page?

document.cookie = "theme=dark";
Show answer
Nothing. Writing to document.cookie adds or updates only the cookie named theme. All the others are untouched.

How do you delete a cookie from JavaScript?

Show answer
Set the same name again (with the same 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/";
Show answer
string. document.cookie always reads back as one string of name=value pairs, however many cookies there are.

// Try It

  1. Click Open Playground and run it. Run it a second time: what changed?
  2. Change the lifetime to 1 day and read the cookie back
  3. Store the text Ava & Ben in a cookie with the helpers and read it back
  4. Delete the cookie and confirm that getCookie returns null
// Video Walkthroughs
Last updated: Oct 11, 2026, 7:21 PM ET