localStorage.setItem('user_name', 'Rohit'); //store a key/value
var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
> Add item
localStorage.setItem('foo', 'bar');
> Get item
localStorage.getItem('foo');
> Remote item
localStorage.removeItem('foo');
> Clear all
localStorage.clear();
localStorage.setItem("user_name", "Bob");
document.body.addEventListener("click", function(){
alert(localStorage.getItem("user_name"))
});
localStorage.setItem(key, val);
var val = localStorage.getItem(key);
localStorage.removeItem(key);
localStorage.clear();
// Check browser support
if (typeof(Storage) !== "undefined") {
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
//-----------------------------------------------------------------
//Set
localStorage.setItem('key', 'value');
/*example*/ localStorage.setItem('name', 'Yeasin Ahammed Apon');
//----------------------------------------------------------------
//Get
localStorage.getItem('key');
/*example*/ localStorage.getItem('name');
//------------------------------------------------------------------
//Remove one
localStorage.removeItem('key');
/*example*/ localStorage.removeItem('name');
//-------------------------------------------------------------------
// Remove all
localStorage.clear();
var KeyName = window.localStorage.key(index);
localStorage.setItem('person', JSON.stringify(person)); //stringify object and store
var retrievedPerson = JSON.parse(localStorage.getItem('person')); //retrieve the object
//localStorage contain key value.
//we can get all localStorage from localStorage tab of storage pannel in inspect.
localStorage.setItem("todo","Feed the cat") //set localStorage with key todo and fee the cat as value
localStorage.clear();// clear the localStorage.
//getting localStorage.
const user = localStorage.getItem('user'); //in parameter we send key value
//adding array and object in localStorage.
//when we use tradition way we get string instead of array and object.
//this issues can be remove by using JSON.stringify and JSON.parse.
const todoList= ["Feed the cat","wash"]
localStorage.setItem("todos",JSON.stringify(todoList)); //changing array to string with []
const retrieved= JSON.parse(localStorage.getItem("todos")); // changing string to object.