Answers for "javascript how to use localstorage"

23

local storage javascript

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';
Posted by: Guest on April-26-2020
7

localStorage

// 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
Posted by: Guest on May-20-2020
3

js local storage

myStorage = localStorage;

localStorage.setItem('myCat', 'Tom');

var cat = localStorage.getItem('myCat');

localStorage.removeItem('myCat');

localStorage.clear();
Posted by: Guest on January-07-2021
0

how to get data from localstorage in javascript

//localStorage is an object, so values are stored by key name
//data from localStorage is always stored in strings
//usually the values at each key are stringified objects in JSON format, therefore we must parse them
var data = JSON.parse(localStorage.getItem("key name"));//make sure the "key name" exists in the localStorage
Posted by: Guest on April-02-2020
0

localstorage.getitem()

//The following snippet accesses the current domain's local Storage object 
//and adds a data item to it using Storage.setItem().
localStorage.setItem('myCat', 'Tom');

//The syntax for reading the localStorage item is as follows:
const cat = localStorage.getItem('myCat');

//The syntax for removing the localStorage item is as follows:
localStorage.removeItem('myCat');

//The syntax for removing all the localStorage items is as follows:
localStorage.clear();
Posted by: Guest on September-05-2020

Code answers related to "javascript how to use localstorage"

Code answers related to "Javascript"

Browse Popular Code Answers by Language