Created
August 31, 2022 00:21
-
-
Save syedjafer/e0e2317b39a36413665e91aced35363b to your computer and use it in GitHub Desktop.
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
// setItem normal strings | |
window.localStorage.setItem("name", "goku"); | |
// getItem | |
const name = window.localStorage.getItem("name"); | |
console.log("name from localstorage, "+name); | |
// Storing an Object without JSON stringify | |
const data = { | |
"commodity":"apple", | |
"price":43 | |
}; | |
window.localStorage.setItem('commodity', data); | |
var result = window.localStorage.getItem('commodity'); | |
console.log("Retrived data without jsonified, "+ result); | |
// Storing an object after converting to JSON string. | |
var jsonifiedString = JSON.stringify(data); | |
window.localStorage.setItem('commodity', jsonifiedString); | |
var result = window.localStorage.getItem('commodity'); | |
console.log("Retrived data after jsonified, "+ result); | |
// remove item | |
window.localStorage.removeItem("commodity"); | |
var result = window.localStorage.getItem('commodity'); | |
console.log("Data after removing the key "+ result); | |
//length | |
console.log("length of local storage " + window.localStorage.length); | |
// clear | |
window.localStorage.clear(); | |
console.log("length of local storage - after clear " + window.localStorage.length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment