1var testObject = { 'one': 1, 'two': 2, 'three': 3 };
2
3// Put the object into storage
4localStorage.setItem('testObject', JSON.stringify(testObject));
5
6// Retrieve the object from storage
7var retrievedObject = localStorage.getItem('testObject');
8
9console.log('retrievedObject: ', JSON.parse(retrievedObject));
1var person = { "name": "billy", "age": 23};
2
3localStorage.setItem('person', JSON.stringify(person)); //stringify object and store
4var retrievedPerson = JSON.parse(localStorage.getItem('person')); //retrieve the object
1localStorage.setItem('user_name', 'Rohit'); //store a key/value
2var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
1localStorage.setItem("user_name", "Bob");
2
3document.body.addEventListener("click", function(){
4 alert(localStorage.getItem("user_name"))
5});
1export const setToLS = (key, value) => {
2 window.localStorage.setItem(key, JSON.stringify(value));
3}
4
5export const getFromLS = key => {
6 const value = window.localStorage.getItem(key);
7
8 if (value) {
9 return JSON.parse(value);
10 }
11}