Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

localstorage in javascript

//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.
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #localstorage #javascript
ADD COMMENT
Topic
Name
9+8 =