local storage and session storage
they are just a database having key and value
key-value pair
1. local storage
- temporary storage: goes off after turning off the browser
console.log('hello');localStorage.setItem('name','rajiv')localStorage.setItem('name2','arjun')localStorage.setItem('name3','dhiraj')let name=localStorage.getItem('name')console.log(name)let name3=localStorage.getItem('name3')console.log(name3)
if you want to store non primitive data type like array you need to stringify to JSON while storing and parse while fetching
while storing
let array=['ram','sita',12]
console.log(array)
localStorage.setItem('stud',JSON.stringify(array))
while fetching
let str=JSON.parse(localStorage.getItem('stud'))console.log(str)
same code and syntax for the Session storage but only thing is it is volatile
clear the localstorage
localstorage.clear()
Comments
Post a Comment