You pick the dark theme on a site, and the next day the site is still dark, even though you never logged in. A choice that small doesn't need to be kept on the server. The browser has its own places to keep notes for every site.
A lasting notebook and a sticky note
The browser has two such places:
- localStorage works like a notebook that lasts: what it holds stays when the tab is closed and the phone is switched off, until it is deleted. Good for a theme and other display choices.
- sessionStorage works like a sticky note on one tab: what it holds is gone as soon as that tab closes. Good for remembering which step of a form you are on. The name is similar, but it is not a server session.
Every site has its own notebook and its own sticky notes. MyShop cannot read another site's notes.
localStorage does not always last: Safari deletes it when you have not clicked, tapped or typed anything on that site during 7 days of using Safari. So important data still belongs on the server.
How they differ from cookies
A cookie is sent along with every request to the server. What localStorage and sessionStorage hold is never sent. Only its own site's pages, in your browser, read it.
TL;DR: The browser has two places to keep notes for every site. localStorage lasts until it is deleted, and sessionStorage is gone when the tab closes. Both belong to one site, and neither is sent to the server.
⚡ A notebook: localStorage
Meaning: localStorage is a place in the browser to keep notes, one for every site. It works like a notebook that lasts: what it holds stays when the tab is closed and the phone is switched off, until it is deleted. Good for a theme and other display choices.
⚡ A sticky note: sessionStorage
Guess first: you have reached step 2 of a form, and that step is noted in sessionStorage, a browser note attached to one tab. The tab is closed, then the site is opened again. Where does the form start?
From the beginning again. The note in sessionStorage was gone as soon as the tab closed.
Meaning: sessionStorage works like a sticky note on one tab. Good for remembering which step of a form you are on.
⚡ How they differ from cookies
Meaning: A cookie is sent along with every request to the server. What localStorage and sessionStorage hold is never sent: only the pages of the site that owns it, in your browser, read it. Every site has its own localStorage and sessionStorage.