Morning:
Afternoon:
this
localStorage
JSON.stringify
JSON.stringify
JSON.parse
localStorage
is storage in your web browser that conforms to Web Storage API. It is scoped by domain, meaning other websites cannot access data stored by your website and vice versa. The data in localStorage
persists in the browser until removed, even if the browser is closed and re-opened.
To set an item, use localStorage.setItem
, and retrieve data using localStorage.getItem
. It is important to remember that values stored will always be strings, so it may be use necessary to use the JSON.stringify
and JSON.parse
methods to set and retrieve non-string data. JSON stands for JavaScript Object Notation. To learn more about JSON, click here.
const myObject = {
thisIsCool: true
}
localStorage.setItem('myObject', myObject)
localStorage.getItem('myObject') // => "[object Object]"
// localStorage saves the result of the implicit myObject.toString() call
localStorage.setItem('myObject', JSON.stringify(myObject))
// calling JSON.stringify converts the object to a JSON string representation
// so it can be stored in localStorage without loss of data
const retrievedObject = localStorage.getItem('myObject') // => "{"thisIsCool":true}"
JSON.parse(retrievedObject) // => {thisIsCool: true}
// JSON.parse converts the retrieved JSON string back into a JavaScript object