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';
// 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
// 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...";
}
localStorage.setItem('name', 'Bob') // make/set a key/value
var username = localStorage.getItem('name') // get the key
console.log(username) // log the key
// This data will be saved even after you close the page
#you must first stringify it with the JSON.stringify() function
localStorage.setItem('items', JSON.stringify(items));
const x = JSON.parse(localStorage.getItem('items'))
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.
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';
// 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
// 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...";
}
localStorage.setItem('name', 'Bob') // make/set a key/value
var username = localStorage.getItem('name') // get the key
console.log(username) // log the key
// This data will be saved even after you close the page
#you must first stringify it with the JSON.stringify() function
localStorage.setItem('items', JSON.stringify(items));
const x = JSON.parse(localStorage.getItem('items'))
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.