Sobes.tech
Junior — Middle

Kuidas erinevad andmete salvestamise mehhanismid LocalStorage, SessionStorage ja cookies brauseris?

sobes.tech AI

Vastus AI-lt

LocalStorage, SessionStorage and cookies are three mechanisms for storing data in the browser, differing in volume, lifetime, and accessibility.

  • LocalStorage

    • Stores data without an expiration date (until manually deleted).
    • The volume is usually around 5-10 MB.
    • Data is available in all tabs and windows of the same domain.
    • Not sent to the server during HTTP requests.
  • SessionStorage

    • Data lives only within a session (as long as the tab is open).
    • The volume is similar to LocalStorage.
    • Data is available only in the current tab/window.
    • Not sent to the server.
  • Cookies

    • Can have a lifetime after which they are deleted.
    • The volume is limited (about 4 KB per cookie).
    • Data is automatically sent to the server with each request to the corresponding domain.
    • Used for authentication, tracking, etc.

Example of using LocalStorage:

localStorage.setItem('user', 'Alice');
const user = localStorage.getItem('user');