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// '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';
1// localStorage for objects, arrays or any data type
2var obj = {
3 firstName: "Bob",
4 lastName: "Jeff",
5 age: 13
6}
7localStorage.setItem("itemname", JSON.stringify(obj)); // Save the obj as string
8var item = JSON.parse(localStorage.getItem("itemname"));
9// ^^ Parse string then set `item` as the obj
1// Set
2localStorage.setItem('monChat', 'Tom');
3
4// Get
5localStorage.getItem('myCat');
6
7// Remove
8localStorage.removeItem('myCat');