0
Q:

local storage

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';
12
function setItem(name, value) {
  localStorage.setItem(name, value);
}
function getItem(name) {
  localStorage.getItem(name);
}
function deletItem(name) {
  localStorage.removeItem(name);
}
function clearStorage() {
  localStorage.clear();
}
3
localStorage.setItem('user_name', 'Rohit'); //store a key/value
var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
32
// using in HTML5 (frontend)

// set
localStorage.setItem('users', 'vilh'); 

users = [
	{
    	id: 1,
        name: 'vilh',
    },
    {
    	id: 2,
        name: 'zidane'
    },
]

// set string 
localStorage.setItem('users', users);	// stored objects
localStorage.setItem('users', JSON.stringify(users));	// stored string

// get
var retrievedUsername = localStorage.getItem('users'); 
2
// Set
localStorage.setItem('monChat', 'Tom');

// Get
localStorage.getItem('myCat');

// Remove
localStorage.removeItem('myCat');
1
localStorage.setItem("user_name", "Bob");

document.body.addEventListener("click", function(){
  alert(localStorage.getItem("user_name"))
});
5
// 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
4
localStorage.setItem('myCat', 'Tom');

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

localStorage.removeItem('myCat');

// Clear all items
localStorage.clear();
2
localStorage.setItem('myCat', 'Tom');
2
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
1

function set(){
    var sendJson = JSON.stringify(allPerfume);
    localStorage.setItem("allPerfume", sendJson);
}

function get() {
    var getJson = localStorage.getItem("allPerfume")
    if (getJson) {
        allPerfume = JSON.parse(getJson);
    }
}
0

New to Communities?

Join the community