Skip to content

Instantly share code, notes, and snippets.

@syedjafer
Created September 1, 2022 15:11
Show Gist options
  • Save syedjafer/2d7c872c1ad7eb279aa8417e39c5f5c7 to your computer and use it in GitHub Desktop.
Save syedjafer/2d7c872c1ad7eb279aa8417e39c5f5c7 to your computer and use it in GitHub Desktop.
// setItem normal strings
window.sessionStorage.setItem("name", "goku");
// getItem
const name = window.sessionStorage.getItem("name");
console.log("name from localstorage, "+name);
// Storing an Object without JSON stringify
const data = {
"commodity":"apple",
"price":43
};
window.sessionStorage.setItem('commodity', data);
var result = window.sessionStorage.getItem('commodity');
console.log("Retrived data without jsonified, "+ result);
// Storing an object after converting to JSON string.
var jsonifiedString = JSON.stringify(data);
window.sessionStorage.setItem('commodity', jsonifiedString);
var result = window.sessionStorage.getItem('commodity');
console.log("Retrived data after jsonified, "+ result);
// remove item
window.sessionStorage.removeItem("commodity");
var result = window.sessionStorage.getItem('commodity');
console.log("Data after removing the key "+ result);
//length
console.log("length of local storage " + window.sessionStorage.length);
// clear
window.sessionStorage.clear();
console.log("length of local storage - after clear " + window.sessionStorage.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment