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));
10
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", "Bob");
2
3document.body.addEventListener("click", function(){
4 alert(localStorage.getItem("user_name"))
5});