Skip to main content Accessibility Feedback

Browser Storage

Cookies

Use document.cookie to set a cookie. It’s a string, using a {KEY}={VALUE}; format. Optionally, you can pass in an expiration date as a timestamp using the expires={VALUE} format.

Cookies can only contain string values.

// Set a cookie named sandwich, with a value of turkey
// Cookie expires on December 31, 2024 at 11:59 and 59 seconds PM
document.cookie = 'sandwich=turkey; expires=Fri, 31 Dec 2024 23:59:59 GMT';

Works back to at least IE6.

For more complex cookie work, I wrote a small helper library that makes working with cookies easier.