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
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
1localStorage.setItem('user_name', 'Rohit'); //store a key/value
2var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
1localStorage.setItem('person', JSON.stringify(person)); //stringify object and store
2var retrievedPerson = JSON.parse(localStorage.getItem('person')); //retrieve the object