Skip to content

Instantly share code, notes, and snippets.

@abachuk
Last active April 4, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abachuk/a35c5c4dd900af0e903bef541018c057 to your computer and use it in GitHub Desktop.
Save abachuk/a35c5c4dd900af0e903bef541018c057 to your computer and use it in GitHub Desktop.
const fruits = {
apples: 3,
bananas: 4,
pears: 1,
blackberries: 8
}
// always check if Storage is available
if (typeof(Storage)) {
// saving the object (or JSON) to localStorage
localStorage.setItem("fruits", JSON.stringify(fruits));
// retrieving the stringified object from localStorage
JSON.parse(localStorage.getItem("fruits"))
// clearing the "fruits" key and value, now if you try to access it - it's null
localStorage.removeItem('fruits');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment