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));
1//Set item
2localStorage.setItem('myCat', 'Tom');
3//Get item
4var cat = localStorage.getItem("myCat");
5//Remove item
6localStorage.removeItem("lastname");
7//Remove all items
8localStorage.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', 'Rohit'); //store a key/value
2var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
1---------------------------------------------
2----- Solving problem with LOCALSTORAGE -----
3---------------------------------------------
4
5// string
6 var texts = "Hello Zidane, welcome to https://learn-tech-tips.blogspot.com";
7 localStorage.setItem('texts', texts);
8 var texts = localStorage.getItem('texts');
9
10// array
11// string
12 var item = {};
13 item[id] = {
14 'id': id,
15 'number': number,
16 'price': price,
17 };
18 localStorage.setItem('items', JSON.stringify(items));
19 var items = JSON.parse(localStorage.getItem('items'));
20
21// Remove item
22localStorage.removeItem("items");
23localStorage.removeItem("texts");
24
25// Remove all items
26localStorage.clear();
27
28