Favorites

DOM Document.cookie Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Document.cookie
DOM Document PropertyThis property is supported by all modern browsers.
Updated 28th November 2026

The cookie property reads all cookies for the page as one string, or writes a single cookie when assigned.

Description

The cookie property gives access to the page's cookies. Reading it returns every cookie visible to the script as one name=value string separated by semicolons. Assigning to it sets or updates one cookie, with optional attributes such as max-age, expires, path, secure and samesite; to delete a cookie, set it with max-age=0.

Cookies marked HttpOnly by the server can't be seen by scripts, which protects session cookies. For data only needed in the browser, localStorage is simpler, and newer browsers also offer the asynchronous cookieStore API.

Cookie values can't contain semicolons, commas or spaces without encoding, so wrap values in encodeURIComponent() when setting them and decode them when reading. Cookies are sent to the server with every matching request, so keep them small.

Syntax

document.cookie
document.cookie = "name=value; max-age=seconds; path=/; samesite=lax"

Value

A string of all cookies visible to the page, in the form "a=1; b=2".

Example

document.cookie = 'theme=dark; max-age=3600; path=/; samesite=lax';
document.cookie = 'lang=en; max-age=3600; path=/';
console.log(document.cookie || '(cookies are blocked in this frame)');
const cookies = Object.fromEntries(document.cookie.split('; ').filter(Boolean).map(c => c.split('=')));
console.log(cookies.theme);

Browser Support

The following information will show you the current browser support for the Document.cookie property. Hover over a browser icon to see the version that first introduced support for this property.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee