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
1// 'key' could be any variable to address your 'value'.
2//'value' could be of any data-type.
3
4// Save data to localStorage
5localStorage.setItem('key', JSON.stringify('value'));
6
7// Get saved data from localStorage
8localStorage.getItem('key') ? JSON.parse(localStorage.getItem('key') : '';
9
10// Remove saved data from localStorage
11localStorage.removeItem('key');
12
13// Remove all saved data from localStorage
14localStorage.clear();
1localStorage.setItem('myCat', 'Tom');
2
3var cat = localStorage.getItem('myCat');
4
5localStorage.removeItem('myCat');
6
7// Clear all items
8localStorage.clear();
9
1function createItem() {
2 localStorage.setItem('nameOfItem', 'value');
3}
4createItem() // Creates a item named 'nameOfItem' and stores a value of 'value'
5
6function getValue() {
7 return localStorage.getItem('nameOfItem');
8} // Gets the value of 'nameOfItem' and returns it
9console.log(getValue()); //'value';
1localStorage.setItem("user_name", "Bob");
2
3document.body.addEventListener("click", function(){
4 alert(localStorage.getItem("user_name"))
5});