Writing a cookie means assigning a name=value string to document.cookie. Each assignment creates or updates one cookie. Optional attributes like path, max-age, Secure, and SameSite follow after semicolons.
Note HttpOnly is server-only
JavaScript cannot set or read HttpOnly cookies. Those come only from server Set-Cookie headers — use them for session tokens.
Remember
document.cookie = "name=value; path=/; max-age=SECONDS; SameSite=Lax"
one assignment → one cookie
omit max-age/expires → session cookie
encodeURIComponent(value) → safe characters
Secure → HTTPS only
Goal
Prefer
Site-wide cookie
path=/
Last 7 days
max-age=604800
Safe values
encodeURIComponent(value)
Modern defaults
SameSite=Lax (+ Secure on HTTPS)
Approach
How to Write Cookies
Build a name=value string, add path and lifetime, encode the value, then assign once.
Choose a name and value; encode the value with encodeURIComponent.
Add path=/ unless you intentionally want a narrower scope.
Add max-age (seconds) for persistence, or omit it for a session cookie.
Add SameSite=Lax (and Secure on HTTPS) for safer defaults.
Piece
Required?
Role
name=value
Yes
Cookie identity and payload
path
Recommended
URL scope — prefer /
max-age / expires
Optional
Persistence vs session
SameSite / Secure
Recommended
Cross-site and HTTPS safety
Pseudocode
Pseudocode
name, value = cookie data
seconds = lifetime in seconds // or null for session
cookie = name + "=" + encodeURIComponent(value)
cookie += "; path=/"
cookie += "; SameSite=Lax"
if seconds is a number:
cookie += "; max-age=" + seconds
if page is HTTPS:
cookie += "; Secure"
document.cookie = cookie // one cookie per assignment
Reference
Cheat Sheet
Keep these write-side facts handy.
Topic
Detail
API
document.cookie = "…"
One write
Creates or updates one cookie
Session
Omit max-age and expires
7 days
max-age=604800
Site-wide
path=/
Encoding
encodeURIComponent(value)
HttpOnly
Server Set-Cookie only
Readback
Attributes are not in document.cookie
Try it
Live Preview
Build a cookie assignment and apply it in this browser. Inspect the result in document.cookie.
Demo cookies use path=/. On plain HTTP, a Secure flag is skipped so the demo still works locally.
Writes themselves are cheap. The cost is bytes on later requests. Prefer short lifetimes and tiny values. For modern async APIs, see the Cookie Store API.
Remember
Key Takeaways
Role: assign document.cookie once per cookie to create or update it.
Defaults: use path=/, max-age when persistent, and SameSite=Lax.
Encode: wrap values with encodeURIComponent.
Secrets: keep auth tokens HttpOnly on the server — not in JS-writable cookies.
One line: write with name=value; path=/; max-age=…; SameSite=Lax, encode values, and never store secrets in script-readable cookies.
Frequently Asked Questions
Assign a name=value string to document.cookie. Each assignment creates or updates one cookie. Example: document.cookie = "theme=dark; path=/; max-age=604800".
No. Writing one cookie does not remove existing cookies with different names. To update, write the same name again with a matching path/domain.
A session cookie has no expires or max-age and is normally removed when the browser session ends. A persistent cookie includes expires or max-age and can survive restarts until it expires or is deleted.
No. HttpOnly cookies can only be created by the server through Set-Cookie headers. JavaScript cannot read or write HttpOnly cookies.
Cookie values cannot safely contain semicolons, spaces, or non-ASCII characters without encoding. encodeURIComponent(value) prevents broken cookies and parsing errors.
path=/ makes the cookie available to every page on the current origin. A narrower path like path=/shop limits visibility to URLs under /shop.