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));
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
1// Check browser support
2if (typeof(Storage) !== "undefined") {
3 // Store
4 localStorage.setItem("lastname", "Smith");
5 // Retrieve
6 document.getElementById("result").innerHTML = localStorage.getItem("lastname");
7} else {
8 document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
9}
1myStorage = localStorage;
2
3localStorage.setItem('myCat', 'Tom');
4
5var cat = localStorage.getItem('myCat');
6
7localStorage.removeItem('myCat');
8
9localStorage.clear();