Sometimes, we want to loop through localStorage in HTML5 and JavaScript.
In this article, we’ll look at how to loop through localStorage in HTML5 and JavaScript.
How to loop through localStorage in HTML5 and JavaScript?
To loop through localStorage in HTML5 and JavaScript,. we can use the Object.keys method to get the keys in localStorage.
For instance, we write
Object.keys(localStorage).forEach((key) => {
console.log(localStorage.getItem(key));
});
to call Object.keys with localStorage to get an array of keys in localStorage.
Then we call forEach with a callback to get the value with the given key with getItem.
Conclusion
To loop through localStorage in HTML5 and JavaScript,. we can use the Object.keys method to get the keys in localStorage.