storing and extracting from local storage code example
Example: storing and extracting from local storage
/* Storing object data in local storage */
let objectData = {
"data": "this is a line of text",
"anArray": [0,1,2,3,4,5],
"aBoolean": true
}
// convert object into a string
let jsonString = JSON.stringify(objectData);
// put it in local storage
localStorage.setItem("storage_key",jsonString);
// we also can use a variable to store the key value instead of using a string directly
let key = "storage_key";
localStorage.setItem(key,jsonString);
/* Retrieving object data from local storage */
let key = "storage_key"; // our key
// get the item from localStorage
let jsonString = localStorage.getItem(key);
// convert it back into an object
let objectData = JSON.parse(jsonString);