Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Browser and Web APIs

The document.cookie property

Reference for document.cookie: read, write and delete cookies, set expires, max-age, path, SameSite and Secure, and know what JavaScript cannot see.

document.cookie is the old, unusual interface to the cookies the browser stores for the current page. Reading it returns one string with every cookie visible to scripts, written as name=value pairs separated by ; . Writing to it sets or updates exactly one cookie per assignment; it does not replace the others, even though it looks like a normal property.

Cookies are also sent to the server with matching requests, which makes them the basis of most logins. JavaScript is poorly suited to managing sensitive ones: a login cookie should be set by the server with the HttpOnly attribute, which makes it invisible to document.cookie and therefore to injected scripts.

JavaScript
document.cookie = "theme=dark; max-age=86400; path=/; samesite=lax";
console.log(document.cookie);

Try it

The preview sandbox has no origin, so reading or writing document.cookie throws there. This first example shows the guard you would use on a real page.

JS Guarded access

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The next example shows the format with two pure helper functions. They work on plain strings, so they run anywhere.

JS Parse and build cookie strings

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
document.cookie                      // read: "a=1; b=2"
document.cookie = "name=value"       // write one cookie
document.cookie = "name=value; attribute; attribute=value"

Attributes you can set from JavaScript:

Attribute Description
Max-Age=seconds Lifetime in seconds. 0 or a negative number deletes the cookie.
Expires=date Absolute expiry as a UTC date string (new Date(...).toUTCString()). Without Max-Age or Expires, the cookie lasts for the browser session.
Path=/ Pages the cookie applies to. Default is the current page's directory, which often surprises people; set / explicitly.
Domain=example.com Share with subdomains. Omit it to limit the cookie to the exact host.
Secure Only sent over HTTPS.
SameSite=Lax / Strict / None Controls sending on cross-site requests. None requires Secure. Current browsers treat a missing value as Lax.

Not available from JavaScript: HttpOnly cookies are hidden from reads, and document.cookie cannot create them.

Return value: the getter returns a string (empty if there are no visible cookies). The setter returns nothing and ignores invalid input silently.

Usage notes

  • One cookie per assignment. document.cookie = "a=1; b=2" sets a with an unknown attribute b, not two cookies.
  • Encode values. Semicolons, commas and spaces break the format. Use encodeURIComponent() when writing and decodeURIComponent() when reading.
  • Delete with the same Path and Domain that created the cookie, or the browser treats it as a different cookie and nothing disappears.
  • Size and count limits are small: around 4 KB per cookie and a few dozen per site. Cookies travel with every matching request, so keep them tiny.
  • Attributes cannot be read back. document.cookie returns names and values only, never the expiry, path or flags.
  • Use localStorage for client-only data. It is not sent to the server and has more room. See localStorage.
  • Consent laws (GDPR, ePrivacy and others) may require permission before setting non-essential cookies.
  • Cookie Store API. cookieStore.get() and cookieStore.set() offer an asynchronous, promise-based alternative and change events, but support is limited, so check a compatibility table.

Browser support

document.cookie works in every browser, though third-party and cross-site cookies are increasingly restricted, and access can fail in sandboxed frames. The Cookie Store API is available only in some browsers.