What are cookie, local storage, and session storage? Differences in storage duration?
sobes.tech AI
Answer from AI
Cookie, localStorage, and sessionStorage are methods of storing data on the client side, but they differ in scope and duration.
-
Cookie — small data sent to the server with each request by the browser. You can set an expiration time using the
ExpiresorMax-Ageattribute. If not set, the cookie is considered a session cookie and is deleted when the browser is closed. Used for authentication, tracking. -
localStorage — stores data in the browser without an expiration time, until the user clears it manually or via code. Data is only accessible on the client side and is not sent to the server.
-
sessionStorage — similar to localStorage, but data only lives within a single browser tab session. Data is deleted when the tab is closed.
Summary of storage durations:
- Cookie: can be a session cookie (deleted when the browser is closed) or have a set expiration.
- localStorage: stored permanently until deleted.
- sessionStorage: stored until the tab is closed.