Get HTML5 localStorage keys Get HTML5 localStorage keys javascript javascript

Get HTML5 localStorage keys


for (var key in localStorage){   console.log(key)}

EDIT: this answer is getting a lot of upvotes, so I guess it's a common question. I feel like I owe it to anyone who might stumble on my answer and think that it's "right" just because it was accepted to make an update. Truth is, the example above isn't really the right way to do this. The best and safest way is to do it like this:

for ( var i = 0, len = localStorage.length; i < len; ++i ) {  console.log( localStorage.getItem( localStorage.key( i ) ) );}


in ES2017 you can use:

Object.entries(localStorage)


I like to create an easily visible object out of it like this.

Object.keys(localStorage).reduce(function(obj, str) {     obj[str] = localStorage.getItem(str);     return obj}, {});

I do a similar thing with cookies as well.

document.cookie.split(';').reduce(function(obj, str){     var s = str.split('=');     obj[s[0].trim()] = s[1];    return obj;}, {});