Local storage and JSON
localStorage is the browser's notebook for your site. JSON.stringify turns an array or object into text to save, and JSON.parse turns it back.
Try it
- JSON playground – Type JSON and see if it is valid, what it describes, how JavaScript reaches each value, and what the mistake is.
Transcript
When you refresh the page, your variables and the tasks on the page disappear. To remember data between visits, the browser has local storage. It is like a small notebook that your website can write in and read later.
Local storage only keeps text. To save an array or an object, turn it into text with J S O N dot stringify. J S O N is a way of writing data as text.
To use the data again, read the text with get item, and turn it back into an array with J S O N dot parse.
Save after every change, and load when the page starts. Now your to-do list remembers its tasks, even after you close the browser.
Remember: local storage is the browser's notebook. J S O N stringify turns data into text to save it, and J S O N parse turns it back.
More in this series
1:2624What is Git?
Git remembers every saved version of a folder. Repository, commit, and the four first commands: git status, add, commit and log.
1:2425The DOM
The DOM is the tree of nodes the browser builds from your HTML. Find a node with document.querySelector and change it with textContent.
1:1926Events in the browser
An event is something that happens on the page. addEventListener waits for it and runs a handler function, shown next to the same idea in Scratch.
1:1827Changing the page
Toggle a class with classList so CSS restyles an element, and build new elements with createElement and append, like a to-do list adding tasks.
1:1528Arrays and objects
An array is a numbered list, an object is a set of labelled values. An array of objects and a loop turn data into cards.
1:2630Fetch and JSON
The browser is the client and fetch sends the request. The server answers with JSON, async and await wait for it, and the Network tab shows what happened.
1:1631Debugging JavaScript
Find bugs in JavaScript: console.log to look, reading an error message and its line, and a breakpoint to pause and step through code in DevTools.