58/361

Describe the difference between a cookie, sessionStorage and localStorage.

Quiz · HTML
Medium
3 min
html

All the above-mentioned technologies are key-value storage mechanisms on the client side. They are only able to store values as strings.

cookielocalStoragesessionStorage
InitiatorClient or server. Server can use Set-Cookie headerClientClient
ExpiryManually setForeverOn tab close
Persistent across browser sessionsDepends on whether expiration is setYesNo
Sent to server with every HTTP requestCookies are automatically being sent via Cookie headerNoNo
Capacity (per domain)4kb5MB5MB
AccessibilityAny windowAny windowSame tab

Note: If the user decides to clear browsing data via whatever mechanism provided by the browser, this will clear out any cookie, localStorage, or sessionStorage stored. It's important to keep this in mind when designing for local persistance, especially when comparing to alternatives such as server side storing in a database or similar (which of course will persist despite user actions).

Adapted from the Front End Interview Handbook by Yangshun Tay and contributors (MIT License). Source

Spotted a mistake or a rights concern? Report an issue