document.localStorage code example

Example 1: 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';

Example 2: localstorage js

// Set
localStorage.setItem('monChat', 'Tom');

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

// Remove
localStorage.removeItem('myCat');

Example 3: local storage javascript

localStorage.setItem('user_name', 'Rohit'); //store a key/value
var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key

Example 4: localstorage

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

Example 5: localstorage javascript

// Store
localStorage.setItem("lastname", "Smith");
// Retrieve

document.getElementById("result").innerHTML = localStorage.getItem("lastname");