JavaScript Curriculum
Web Storage
easyThe coffee shop cart keeps emptying on page refresh. User preferences reset every visit. Web Storage fixes this — persist the cart, theme preference, and last-viewed category without a server.
Web Storage
The browser gives you two storage buckets you can read and write with JavaScript. No server, no cookies, no expiry headers — just a simple key/value store that survives page reloads.
localStorage — Interactive Explorer
Set, get, remove, and clear items in the simulated localStorage:
localStorage contents
localStorage
- •Persists forever — survives page closes, browser restarts
- •Scoped to origin (domain + protocol + port)
- •Synchronous — blocks the main thread if reading large data
- •~5MB limit. Strings only — JSON.stringify for objects
localStorage vs sessionStorage
Same API, very different lifetime — knowing when to use which matters:
<storagevsession></storagevsession>
Storing Objects and Arrays with JSON
localStorage only holds strings — JSON bridges the gap:
Your Challenge
Build saveCart(items) — calls localStorage.setItem('cart', JSON.stringify(items)). Build loadCart() — reads and parses, returns [] if missing or invalid. On page load call loadCart() and render the items. Wire the order button to add to cart and call saveCart.
Challenge
Build a cart that persists across page refreshes. On page load, read the cart from localStorage with JSON.parse. On every cart change, write it back with JSON.stringify. Add a clear button that calls localStorage.removeItem('cart').
Web Storage
easyThe coffee shop cart keeps emptying on page refresh. User preferences reset every visit. Web Storage fixes this — persist the cart, theme preference, and last-viewed category without a server.
Web Storage
The browser gives you two storage buckets you can read and write with JavaScript. No server, no cookies, no expiry headers — just a simple key/value store that survives page reloads.
localStorage — Interactive Explorer
Set, get, remove, and clear items in the simulated localStorage:
localStorage contents
localStorage
- •Persists forever — survives page closes, browser restarts
- •Scoped to origin (domain + protocol + port)
- •Synchronous — blocks the main thread if reading large data
- •~5MB limit. Strings only — JSON.stringify for objects
localStorage vs sessionStorage
Same API, very different lifetime — knowing when to use which matters:
<storagevsession></storagevsession>
Storing Objects and Arrays with JSON
localStorage only holds strings — JSON bridges the gap:
Your Challenge
Build saveCart(items) — calls localStorage.setItem('cart', JSON.stringify(items)). Build loadCart() — reads and parses, returns [] if missing or invalid. On page load call loadCart() and render the items. Wire the order button to add to cart and call saveCart.
Challenge
Build a cart that persists across page refreshes. On page load, read the cart from localStorage with JSON.parse. On every cart change, write it back with JSON.stringify. Add a clear button that calls localStorage.removeItem('cart').