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
- Do not confuse the string
'null'with the valuenull. - Validate stored data because users or older versions of your app may have written it.
- Remember that storage can be unavailable.