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('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';
1function setItem(name, value) {
2 localStorage.setItem(name, value);
3}
4function getItem(name) {
5 localStorage.getItem(name);
6}
7function deletItem(name) {
8 localStorage.removeItem(name);
9}
10function clearStorage() {
11 localStorage.clear();
12}
1localStorage.setItem('user_name', 'Rohit'); //store a key/value
2var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
1localStorage.setItem(key, val);
2var val = localStorage.getItem(key);
3localStorage.removeItem(key);
4localStorage.clear();