Search code examples
javascripthtmlkeylocal-storage

Get HTML5 localStorage keys


I'm just wondering how to get all key values in localStorage.


I have tried to retrieve the values with a simple JavaScript loop

for (var i=1; i <= localStorage.length; i++)  {
   alert(localStorage.getItem(i))
}

But it works only if the keys are progressive numbers, starting at 1.


How do I get all the keys, in order to display all available data?


Solution

  • in ES2017 you can use:

    Object.entries(localStorage)