Last active
December 11, 2022 07:43
-
-
Save Octagon-simon/df95609297f0a3d95144eb85f56eb5b4 to your computer and use it in GitHub Desktop.
All localStorage methods and how to can use them
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
//LOCAL STORAGE | |
//Saves data permanently to the user's browser | |
//syntax | |
//localStorage.setItem("key", "value") | |
//save an item | |
localStorage.setItem('name', 'simon'); | |
//save multiple items in an object | |
localStorage.setItem('data', JSON.stringify({ | |
"name": "Simon", | |
"stack": "MERN" | |
})) | |
//how many items are saved | |
localStorage.length //2 | |
//get one item | |
localStorage.getItem('name') //simon | |
//retrieve the name of the first saved item | |
localStorage.key(0) //name | |
//retrieve the name of the second saved item | |
localStorage.key(1) //data | |
//get multiple items saved as object | |
JSON.parse( localStorage.getItem('data') ) | |
//or | |
JSON.parse( localStorage.data ) | |
//{name : "Simon", "stack" : "MERN"} | |
//delete an item | |
localStorage.removeItem('name') | |
//delete everything | |
localStorage.clear() | |
//Simon Ugorji 🚀 (Octagon) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment