Created
October 22, 2019 20:21
-
-
Save winnekes/9c6f103742678021be8a270bf9c15bbe to your computer and use it in GitHub Desktop.
localStorage Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! ๐ I'm a localStorage fan myself ๐