← All snippets

localStorage Helper

Typed wrapper around localStorage that auto-parses JSON and silently catches quota / private-mode errors.

utilities

Code

const store = {
  get(key, fallback = null) {
    try { return JSON.parse(localStorage.getItem(key)) ?? fallback }
    catch { return fallback }
  },
  set(key, val) {
    try { localStorage.setItem(key, JSON.stringify(val)) } catch {}
  },
}