localStorage vs sessionStorage — and why the cart vanishes in a new tab

You built an online store. You keep the cart in sessionStorage — the AI suggested it, sounds reasonable. A shopper adds three items. Then they Ctrl-click "Checkout" to open it in a new tab. The cart there is empty.
They close the new tab. They right-click the old one and choose "Duplicate". In the copy the cart is intact — all three items.
That's not a glitch. It's by design. localStorage and sessionStorage share the same code but live completely different lives. Let's see what the difference is and how to choose.
What they have in common
First, where they're the same. Both are browser "key → string" stores with the same API:
localStorage.setItem('theme', 'dark')
sessionStorage.setItem('step', '2')
localStorage.getItem('theme') // 'dark'
sessionStorage.removeItem('step')
Also shared:
- They store only strings. An object has to be turned into JSON:
JSON.stringifyon write,JSON.parseon read. Otherwise you get'[object Object]'. - They're tied to the site. More precisely, to protocol + domain + port.
localhost:3000andlocalhost:5173are different stores. That's why data "disappears" when the dev server starts on another port. - The limit is about 5 MB per site. Overflow it and you get a
QuotaExceededError. - They're synchronous. A big write briefly freezes the page.
- Any script on the page can read them. More on that in its own section below.
The main difference — how long data lives
localStorage lives until someone deletes it. Close the tab, the browser, reboot the computer — the data is still there. It's shared by all tabs of the site. Change the theme in one, and the others read the new value too.
sessionStorage lives as long as the tab does. Each tab gets its own separate box:
- page reload — data stays;
- following links within the site in the same tab — stays;
- tab closed — data is deleted;
- site opened in a new tab, even via a link — in modern browsers it's empty;
- tab duplicated — the new one gets a copy. From then on the two copies live separately and don't sync.
Hence the cart story. A new tab is a new empty box. A duplicated tab is a copy of the old box.
How tabs learn about changes
localStorage has a nice trick — the storage event. When one tab changes a value, the other tabs of the same site get notified:
window.addEventListener('storage', (e) => {
if (e.key === 'token' && e.newValue === null) logoutHere()
})
That's how "log out everywhere" works: log out in one tab, and the others log out too. The tab that made the change doesn't receive the event itself.
With sessionStorage there's nothing to sync: each tab has its own data.
The Safari trap, and what you shouldn't store anywhere
"Forever" for localStorage comes with a caveat. Safari deletes script-written data if the person hasn't visited the site for 7 days. That's its ITP anti-tracking protection. If a user comes back after two weeks and "everything reset" — that's why. Keep anything important on the server. Other reasons browser data disappears are in what is a cookie.
And the main rule for both stores. Don't put login tokens or secrets there. Any script on the page can read both localStorage and sessionStorage. One XSS hole or a compromised library — and the token is gone. For a session, a cookie with the HttpOnly flag is safer: JavaScript can't see it at all. More in what is a JWT.
Which one fits whom
A verdict with no fence-sitting:
- Use localStorage if the data must survive closing the browser and be the same in every tab. Theme (as in the dark mode guide), language, a collapsed menu, a comment draft, "don't show this tip again".
- Use sessionStorage if the data belongs to one specific task in one tab. The step of a multi-step form, scroll position for going back, filters that shouldn't "leak" into the next tab. Someone fills in two applications in two tabs — and they won't get mixed up.
- Keep the cart in localStorage, or better, on the server. Shoppers expect one cart across the whole site.
- Neither — passwords, tokens, card data.
Unsure? Ask yourself: "If the person opens the site in a new tab, do they expect to see this there?" Yes — localStorage. No — sessionStorage.
Does sessionStorage survive a reload?
Yes. Reloads and navigation within the same tab don't touch it. The data is deleted when the tab is closed.
Can tabs share sessionStorage?
Not directly — each tab has its own store. The exception is duplicating a tab: the new one gets a copy. Need shared data — use localStorage or BroadcastChannel.
Which is more secure — localStorage or sessionStorage?
Neither. Both are available to any script on the page. sessionStorage just forgets data sooner. For tokens, use a cookie with HttpOnly.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





