xxxxxxxxxx
function createItem() {
localStorage.setItem('nameOfItem', 'value');
}
createItem() // Creates a item named 'nameOfItem' and stores a value of 'value'
function getValue() {
return localStorage.getItem('nameOfItem');
} // Gets the value of 'nameOfItem' and returns it
console.log(getValue()); //'value';
xxxxxxxxxx
var person = { "name": "billy", "age": 23};
localStorage.setItem('person', JSON.stringify(person)); //stringify object and store
var retrievedPerson = JSON.parse(localStorage.getItem('person')); //retrieve the object
xxxxxxxxxx
localStorage.setItem('nombre_color', 'Rojo'); // envio de data al "localStorage"
localStorage.getItem("nombre_color");// captura de data del "localStorage"
localStorage.removeItem('nombre_color'); // Eliminar variable especifica
localStorage.clear(); // Eliminar todas las variables
xxxxxxxxxx
localStorage.setItem('user_name', 'Rohit'); //store a key/value
var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
xxxxxxxxxx
// localStorage for objects, arrays or any data type
var obj = {
firstName: "Bob",
lastName: "Jeff",
age: 13
}
localStorage.setItem("itemname", JSON.stringify(obj)); // Save the obj as string
var item = JSON.parse(localStorage.getItem("itemname"));
// ^^ Parse string then set `item` as the obj
xxxxxxxxxx
> Add item
localStorage.setItem('foo', 'bar');
> Get item
localStorage.getItem('foo');
> Remote item
localStorage.removeItem('foo');
> Clear all
localStorage.clear();
xxxxxxxxxx
function setItem(name, value) {
localStorage.setItem(name, value);
}
function getItem(name) {
localStorage.getItem(name);
}
function deletItem(name) {
localStorage.removeItem(name);
}
function clearStorage() {
localStorage.clear();
}
xxxxxxxxxx
localStorage.setItem("user_name", "Bob");
document.body.addEventListener("click", function(){
alert(localStorage.getItem("user_name"))
});
xxxxxxxxxx
localStorage.setItem(key, val);
var val = localStorage.getItem(key);
localStorage.removeItem(key);
localStorage.clear();
xxxxxxxxxx
// Storing data in local storage
localStorage.setItem('key', 'value');
// Retrieving data from local storage
const storedValue = localStorage.getItem('key');
console.log(storedValue);
// Removing data from local storage
localStorage.removeItem('key');