Reading cookies means inspecting document.cookie — a single semicolon-separated string of name=value pairs visible to scripts on the current page. Attributes like path, expires, and HttpOnly are never included.
Note Write ≠ read
document.cookie is an accessor: assigning sets one cookie; reading returns the whole jar of script-visible name=value pairs — not the string you just wrote.
Remember
document.cookie
→ "username=JohnDoe; theme=dark"
split on ";" → trim → split on first "="
getCookie("theme") → "dark"
missing / HttpOnly / wrong path → null (or absent)
Goal
Prefer
See all visible cookies
document.cookie
One cookie by name
getCookie(name) helper
Encoded values
decodeURIComponent(value)
Many lookups
Parse once into an object
Approach
How to Read Cookies
Read the string, parse pairs carefully, and decode values you encoded on write.
Read document.cookie (empty string if none are visible).
Split on ";", then trim() each segment (spaces are common).
Split each segment on the first= only — values may contain more equals.
Use decodeURIComponent when the value was written with encodeURIComponent.
Piece
Required?
Role
document.cookie
Yes
Raw semicolon-separated string
Split + trim
Yes
Isolate each name=value
First = only
Yes
Keeps = inside values intact
decodeURIComponent
Recommended
Restores encoded characters
Pseudocode
Pseudocode
raw = document.cookie // "a=1; b=2" or ""
target = cookie name to find
for each part in raw.split(";"):
part = trim(part)
eq = index of first "=" in part
if eq == -1: continue
name = part[0 .. eq)
value = part[eq+1 .. end]
if name == target:
return decode(value)
return null // missing / not visible
Reference
Cheat Sheet
Keep these read-side facts handy.
Topic
Detail
API
document.cookie (getter)
Format
name=value; name2=value2
Metadata
No path / expires / HttpOnly in the string
Missing cookie
Helper returns null (or omit from object)
HttpOnly
Never readable from JavaScript
Equals in value
Split on first = only
Encoding
Pair encodeURIComponent / decodeURIComponent
Try it
Live Preview
Seed demo cookies, then look up a name with a small getCookie parser — same idea as the examples below.
Seeds theme, username, and encoded greeting with path=/. HttpOnly cookies would never appear here.
Result
Click “Seed cookies”, then “Read”.
document.cookie → (current jar)
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Read All Cookies as a String
Log the full document.cookie value after setting two cookies.
Start empty. Build a plain object for every visible cookie.
Parse each pair. Same trim + first-= rules as getCookie.
Decode values. Store human-readable strings when encoding was used.
Duplicate names. Later keys overwrite earlier ones in the object — path duplicates can look ambiguous in one string.
Edge Cases & Pitfalls
Check these before trusting cookie reads in production.
HttpOnly
HttpOnly never appears
Session tokens set with HttpOnly are hidden from document.cookie on purpose — DevTools can show them; scripts cannot.
path
Wrong path looks like “missing”
A cookie scoped to /shop is invisible on /js/cookies/read. See Cookie Path.
parse
Split on every =
Values can contain equals signs. Always use the first = only.
meta
No expiry or path in reads
Inspect Application → Cookies in DevTools for attributes. The JS string never includes them.
Compatibility
Browser Support
Reading cookies via document.cookie is a long-standing DOM feature available in all browsers that run JavaScript on web pages.
✓ Baseline · DOM
document.cookie (read)
Supported across Chrome, Firefox, Safari, Edge, and legacy IE. Parsing helpers are pure JavaScript — no special browser API required.
99%Universal API
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
document.cookieExcellent
Bottom line: Safe for production. For high-frequency cookie work in modern apps, consider the async Cookie Store API as an alternative to sync reads.
Analysis
Performance Notes
Approach
Cost
Best for
One getCookie
O(n) over cookie count
Occasional lookups
Parse once to object
O(n) once
Many reads on the same page
Hot-loop document.cookie
Sync; can block
Avoid — cache the parsed result
MDN notes that document.cookie is synchronous and can block the main thread. Prefer parsing once per page load. For client-only data, consider localStorage; for modern async cookie APIs, see the Cookie Store API.
Parse carefully: trim segments; split on the first =.
Decode: pair encodeURIComponent on write with decodeURIComponent on read.
Limits: HttpOnly, wrong path, and expired cookies look “missing.”
One line: read document.cookie, parse with first-= splits, decode when needed, and remember HttpOnly stays invisible.
Frequently Asked Questions
Use document.cookie. It returns one semicolon-separated string of name=value pairs visible to scripts on the current page, such as "theme=dark; lang=en".
Split document.cookie on semicolons, trim each part, split on the first equals sign, then compare the name. A small getCookie(name) helper wraps this loop.
HttpOnly cookies, wrong path scope, expired cookies, or cookies on another domain are not exposed through document.cookie even if DevTools shows them.
Yes, when values were stored with encodeURIComponent. It restores spaces, commas, and Unicode characters safely.
No. The string only includes names and values. Attributes like expires, path, Secure, and HttpOnly are never returned to script.
Split on the first equals sign only: const eq = part.indexOf("="); const value = part.slice(eq + 1). Splitting on every equals truncates the value.