Web Storage
Web storage lets a browser remember small bits of data.
localStorage keeps data until you clear it.
sessionStorage keeps data only for the current tab session.
localStorage
sessionStorage
setItem and getItem
removeItem and clear
JSON for objects
localStorage
This is like a toy locker that stays locked overnight.
Close the browser. Open it later. The data can still be there.
sessionStorage
This is like a desk tray for today only.
Close the tab, and that tray is cleared.
setItem and getItem
Keys and values are stored as strings.
setItem saves. getItem reads.
removeItem and clear
removeItem deletes one key.
clear deletes everything in that storage.
Save objects with JSON
Storage only keeps strings.
Use JSON.stringify to pack an object. Use JSON.parse to unpack it.
Missing keys
If a key is not there, getItem returns null.
Check for null before you use the value.
Tip: Use localStorage for lasting settings. Use sessionStorage for short lived tab data.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.