Web Storage: getItem()

The getItem() method returns the string saved under a key, or null when that key does not exist.

const theme = localStorage.getItem('theme') ?? 'light';

Choose a sensible default for a missing value. If you stored JSON, parse it only after checking that the string has the expected shape.

Common mistakes