js store in local storage 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: local storage javascript
function createItem() {
localStorage.setItem('nameOfItem', 'value');
}
createItem()
function getValue() {
return localStorage.getItem('nameOfItem');
}
console.log(getValue());
Example 3: js save local storage
localStorage.setItem('myCat', 'Tom');
var cat = localStorage.getItem("myCat");
localStorage.removeItem("lastname");
localStorage.clear();
Example 4: localStorage
var obj = {
firstName: "Bob",
lastName: "Jeff",
age: 13
}
localStorage.setItem("itemname", JSON.stringify(obj));
var item = JSON.parse(localStorage.getItem("itemname"));
Example 5: javascript get local storage
var Item = localStorage.getItem('youritem');
Example 6: local storage
As the answers here already talk about the coding aspect. I will talk about
the concept.
Local storage is basically an object stored in the specific browser you are
using in that moment. And thus it is tied to that browser in that device. It's
duration is infinite so it never expires
If you only want to store data that only lasts for that browser session(
starts when you open a window and ends when you close it) then the best choice
is sessionStorage