Last active
February 16, 2023 19:16
-
-
Save enggsuraj/4f47a54c5232e322a54c8d78a7bc6da2 to your computer and use it in GitHub Desktop.
Local Storage in Javascript
This file contains 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
// USER OBJECT | |
const user = { | |
name: "blogtheorem", | |
ID: 123, | |
isCoding: true, | |
}; | |
// CRUD OPERATION | |
//1. CREATE | |
const userJSON = JSON.stringify(user); | |
localStorage.setItem("user", userJSON); | |
// 2. READ | |
const userString = localStorage.getItem("user"); | |
const userJSONdata = JSON.parse(userString); | |
console.log(`Created: ${userJSONdata.name} ${userJSONdata.ID}`); | |
// 3. UPDATE | |
userJSONdata.name = "blog"; | |
userJSONdata.ID = "321"; | |
const updateuserJSON = JSON.stringify(userJSONdata); | |
localStorage.setItem("user", updateuserJSON); | |
console.log(`Updated : ${userJSONdata.name} ${userJSONdata.ID}`); | |
// 4. DELETE | |
localStorage.removeItem("user"); | |
// CLEAR | |
localStorage.clear(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment