Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arodu/cee458d73e5ab0cf8f72adf116352468 to your computer and use it in GitHub Desktop.
Save arodu/cee458d73e5ab0cf8f72adf116352468 to your computer and use it in GitHub Desktop.
Local storage with JSON parse and stringify
var animal = {
name: 'Karl',
type: 'cat',
color: 'black',
age: 7
};
//convert JSON animal into a string
var dehydratedAnimal = JSON.stringify(animal);
//save it with local storage
window.localStorage.setItem('animal', dehydratedAnimal);
//get 'animal' and rehydrate it (convert it back JSON)
var rehydratedAnimal = JSON.parse(window.localStorage.getItem('animal'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment