Skip to content

Instantly share code, notes, and snippets.

@iamakimmer
Created June 8, 2016 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamakimmer/128f08a2f4aaee4a2d96bc83675971bf to your computer and use it in GitHub Desktop.
Save iamakimmer/128f08a2f4aaee4a2d96bc83675971bf to your computer and use it in GitHub Desktop.
localStorage
localStorage.clear(); //clears all data from localStorage
localStorage.setItem('name', 'john'); //set name to john
localStorage.setItem('user_id', '124242'); //set user_id to 124242
var n = localStorage.getItem('name'); //returns name and set to variable n;
var u = localStorage.getItem('user_id'); //returns 124242 and set to variable u;
console.log(n) //john
console.log(u) //124242
var myArray = [1,2,3,4,5]; //create an array
var myArrayString = JSON.stringify(myArray); //convert array to string
localStorage.setItem('myArray', myArrayString); //set the string representation of the array to myArray
var arr = localStorage.getItem('myArray') //return string representation of the array
typeof arr; //returns string
arr = JSON.parse(arr); //conver string to object
typeof arr //returns object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment