Skip to content

Instantly share code, notes, and snippets.

@winnekes
Created October 22, 2019 20:21
Show Gist options
  • Save winnekes/9c6f103742678021be8a270bf9c15bbe to your computer and use it in GitHub Desktop.
Save winnekes/9c6f103742678021be8a270bf9c15bbe to your computer and use it in GitHub Desktop.
localStorage Example
let myDetails = {
name: 'Simona',
hobbies: ['reading', 'sleeping'],
cats: ['Fetti', 'Diego'],
loves: 'Robots'
};
// add key/value pairs to your localStorage
// to store JS objects you need to serialise them first
localStorage.setItem('details', JSON.stringify(myDetails));
localStorage.setItem('favouriteBook', '"Until I Find You", by John Irving');
localStorage.setItem('currentMood', 'grumpy');
// it's a rainy Saturday evening, what am I going to read?
localStorage.getItem('favouriteBook');
// Remember to deserialise to convert to an object again
JSON.parse(localStorage.getItem('details'));
// someone gave me chocolate, I am no longer grumpy!
localStorage.removeItem('currentMood');
// and now to remove ALL entries from my localStorage
localStorage.clear();
@Remzi1993
Copy link

Nice! 👍 I'm a localStorage fan myself 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment