Created
February 11, 2017 17:00
-
-
Save mburakerman/7cef2f7d387569e306a6a48c297c6188 to your computer and use it in GitHub Desktop.
An Overview of localStorage
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
/* WITH JSON */ | |
// Create with JSON | |
var user = { name: 'Mehmet Burak Erman', age: 22 } | |
localStorage.setItem('user', JSON.stringify(user)); | |
// Read | |
console.log( JSON.parse(localStorage.getItem('user')) ); | |
// or | |
console.log( JSON.parse(localStorage.getItem('user')).name ); | |
// Update (age updated) | |
var updatedUser = { name: 'Ire Aderinokun', age: 30 } | |
localStorage.setItem('user', JSON.stringify(updatedUser)); | |
// Delete | |
localStorage.removeItem('user'); | |
/* WITHOUT JSON */ | |
// Create withot JSON | |
var black_color="#222"; | |
localStorage.setItem("color",black_color); | |
// Read | |
if(localStorage.getItem("color")) { | |
console.log(localStorage.getItem("color")); | |
} | |
// Update | |
black_color="#333"; | |
localStorage.setItem("color",black_color); | |
// Delete | |
localStorage.removeItem('color'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment