Cookie lifetime attributes tell the browser how long to keep a cookie. Omit both for a session cookie; set expires (UTC date) or max-age (seconds) for a persistent cookie. Prefer max-age for new code.
Warning Session restore
Many browsers restore tabs on restart and can keep session cookies alive — do not rely on “session” expiry for security-sensitive data.
Remember
document.cookie = "name=value; path=/; max-age=SECONDS"
document.cookie = "name=value; path=/; expires=" + date.toUTCString()
no expires / max-age → session cookie
max-age=0 → delete (same path)
both set → max-age wins
Goal
Prefer
Relative lifetime
max-age=604800 (7 days)
Absolute deadline
expires= + toUTCString()
Temporary UI flag
Omit both (session cookie)
Remove a cookie
max-age=0 + matching path
Approach
How to Use Cookie Lifetime
Pick a lifetime style, write one cookie assignment, and re-send lifetime on every update.
Decide session (omit lifetime) vs persistent (max-age or expires).
Prefer max-age in seconds — avoid timezone mistakes with date strings.
Always include the same path (usually path=/) when setting or deleting.
On update, re-send max-age/expires or the cookie becomes a session cookie.
Piece
Required?
Role
name=value
Yes
Cookie identity and payload
max-age or expires
Optional
Persistence; omit for session
path
Recommended
Must match when deleting/updating
SameSite / Secure
Recommended
Cross-site and HTTPS safety
Pseudocode
Pseudocode
name, value = cookie data
seconds = lifetime in seconds // or null for session
cookie = name + "=" + encode(value) + "; path=/"
if seconds is a number:
if seconds <= 0:
cookie += "; max-age=0" // delete
else:
cookie += "; max-age=" + seconds
document.cookie = cookie // one cookie per assignment
Reference
Cheat Sheet
Keep these lifetime facts handy while writing cookies.
Topic
Detail
Session
Omit expires and max-age
1 hour
max-age=3600
1 day
max-age=86400
7 days
max-age=604800
30 days
max-age=2592000
Delete
max-age=0 (or past expires)
Precedence
If both set, max-age wins
Read expiry in JS
Not available — use DevTools
Try it
Live Preview
Pick a lifetime mode. The demo builds a document.cookie assignment and can apply it in this browser.
Uses a demo cookie named ctfDemo. Inspect Expires / Max-Age in DevTools → Application → Cookies.
Create first. A persistent username cookie is written with max-age.
Delete is a write. There is no separate delete API — overwrite with empty value.
Zero lifetime.max-age=0 (or a past expires) removes it immediately.
Match path.path=/ must match the original or the old cookie stays.
Edge Cases & Pitfalls
Check these before shipping cookie lifetime logic.
trap
Update without lifetime
Rewriting a cookie without max-age/expires turns a persistent cookie into a session cookie.
path
Delete path must match
max-age=0 only removes the cookie if path (and domain if used) match the original write.
hidden
Expiry is not in reads
document.cookie returns name=value only. Use DevTools for Expires / Max-Age columns.
session
Session restore survives “close”
Browsers that restore tabs can keep session cookies. For sensitive data, use short max-age and server-side invalidation.
Compatibility
Browser Support
expires and max-age are standard cookie attributes available wherever document.cookie works. Prefer max-age for new JavaScript; expires remains useful for absolute deadlines.
✓ Baseline · HTTP cookies
Cookie Expires
Supported across Chrome, Firefox, Safari, Edge, and legacy IE. Both attributes are part of the long-standing cookie model used by document.cookie.
99%Universal attributes
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
expires / max-ageExcellent
Bottom line: Safe for production. Prefer max-age for relative lifetimes; verify persistence and deletion in DevTools when behavior looks wrong.
Analysis
Performance Notes
Operation
Cost
Best for
Write one cookie
O(1) assign
Setting / refreshing lifetime
Read document.cookie
Sync string build
Occasional reads — avoid hot loops
Many long-lived cookies
Request overhead
Prefer short lifetimes; keep values small
Lifetime attributes themselves are cheap. The cost is cookies riding on every matching request — keep durations short and values small. For client-only data, consider localStorage instead of long-lived cookies.
Remember
Key Takeaways
Role:expires / max-age control how long a cookie lives.
Prefer max-age: relative seconds, less error-prone than UTC date strings.
Delete: overwrite with max-age=0 and the same path.
Re-send lifetime: updates without it silently become session cookies.
One line: use max-age for persistence, omit lifetime for session UI flags, and delete with max-age=0 + matching path.
Frequently Asked Questions
Both set cookie lifetime. expires is an absolute UTC date string (toUTCString). max-age is relative seconds from now. Prefer max-age — it is simpler and takes precedence if both are set.
A cookie set without expires or max-age. Browsers normally remove it when the session ends, but session restore can keep it — do not treat “session” as a security guarantee.
Overwrite it with the same name and path, setting max-age=0 (or a past expires date). Example: document.cookie = "name=; path=/; max-age=0".
No. document.cookie returns only name=value pairs. Check Expires / Max-Age in DevTools under Application → Cookies.
It becomes a session cookie, even if the previous version was persistent. Always re-send lifetime attributes when updating.
604800 seconds (7 × 24 × 60 × 60). Common values: 1 hour = 3600, 1 day = 86400, 30 days = 2592000.