Прескочи на главни садржај
← ⚡ JavaScript & the browser·Модул C9 · Лекција 1
ЗадатакWrite log(key) helper that logs key + '=' + localStorage.getItem(key). Set 'theme'='dark', 'locale'='uk', then log each. Remove 'theme', log it again (should be 'theme=null').

localStorage — read, write, remove

75 XP6 min
Теорија

Persistent key/value storage

localStorage survives page reloads. Up to ~5 MB per origin. Both keys and values are strings — numbers and objects need to be serialized.

localStorage.setItem("theme", "dark");
const t = localStorage.getItem("theme"); // "dark"
localStorage.removeItem("theme");
localStorage.clear();                     // nuclear option

Common shape

  • User prefs (theme, locale, font-size).
  • Session resumption ("you were on lesson-42").
  • Form draft autosave.

What NOT to store

  • Sensitive data (anyone with devtools can read it).
  • Large blobs (use IndexedDB — there's a quota).
  • Anything you'd lose sleep over if the user clicked "Clear browsing data".

The string trap

localStorage.setItem("count", 7) silently stringifies — when you read it back you get "7", not 7. Use JSON.stringify / JSON.parse for non-strings (lesson C9-02).

🔒

Региструј се да почнеш са кодом

Теорија је отворена за све. Едитор, преглед уживо и провера се откључавају са 7-дневним бесплатним пробним периодом — потребна картица.

Регистрација — бесплатан пробни период →

Првих 10 лекција у сваком путу су бесплатне. Без картице.

← ПретходнаСледећа лекција →

Један савет за Python или веб дневно — на имејл

Кратко, ручно писано, без спама. Одјава у једном клику.