javascript localstorage code example
Example 1: javascript store object in local storage
var person = { "name": "billy", "age": 23};
localStorage.setItem('person', JSON.stringify(person));
var retrievedPerson = JSON.parse(localStorage.getItem('person'));
Example 2: save to local storage
localStorage.setItem('myCat', 'Tom');
var cat = localStorage.getItem('myCat');
localStorage.removeItem('myCat');
localStorage.clear();
Example 3: local storage javascript
function createItem() {
localStorage.setItem('nameOfItem', 'value');
}
createItem()
function getValue() {
return localStorage.getItem('nameOfItem');
}
console.log(getValue());
Example 4: localstorage js
localStorage.setItem('monChat', 'Tom');
localStorage.getItem('myCat');
localStorage.removeItem('myCat');
Example 5: javascript localstorage
localStorage.setItem("user_name", "Bob");
document.body.addEventListener("click", function(){
alert(localStorage.getItem("user_name"))
});
Example 6: javascript localstorage
var KeyName = window.localStorage.key(index);