tezvyn:

Storing Objects in Web Storage: The JSON Step

Source: developer.mozilla.orgbeginner

Web Storage only stores strings. To save complex data like objects, you must first serialize them with `JSON.stringify()`. This is essential for persisting user settings or session info.

Web Storage is a key-value store with a catch: it only holds strings. To store complex data like a user settings object, you must first serialize it into a string using `JSON.stringify()`. This is how you persist application state or cache data in the browser. The main footgun is forgetting to use `JSON.parse()` when retrieving the data. Your code will receive a string instead of an object, leading to `TypeError`s when you try to access properties that don't exist on a string.

Read the original → developer.mozilla.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Storing Objects in Web Storage: The JSON Step · Tezvyn