Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

localstorage vs sessionstorage

// localstorage vs sessionstorage
// localStorage and sessionStorage are almost identical and have the same API. 
// The difference is that with sessionStorage, the data is persisted only until 
// the window or tab is closed. 
// With localStorage, the data is persisted until the user manually clears the
// browser cache or until your web app clears the data. 

// # localStorage API example: 
// goto devtools & click Application => localStorage to find the key value list
// Add data to localStorage
localStorage.setItem("ls-key", "ls-value"); 

// Get saved data from localStorage 
const value1 = localStorage.getItem("ls-key"); 

// Remove saved data from sessionStorage
localStorage.removeItem('ls-key');

// Remove all saved data from sessionStorage
localStorage.clear();

// # sessionStorage API example:
// goto devtools & click Application => sessionStorage
// Add data to sessionStorage
sessionStorage.setItem("s-key", "s-value"); 

// Get saved data from sessionStorage
const sessionValue = sessionStorage.getItem("s-key");  

// Remove saved data from sessionStorage
sessionStorage.removeItem('s-key');

// Remove all saved data from sessionStorage
sessionStorage.clear();
 
PREVIOUS NEXT
Tagged: #localstorage #sessionstorage
ADD COMMENT
Topic
Name
6+7 =