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 use Object.keys
and forEach
.
For instance, we write
Object.keys(localStorage).forEach((key) => {
console.log(localStorage.getItem(key));
});
to call Object.keys
with localStorage
to return the keys of the entries in local storage as an array.
Then we call forEach
with a callback toi get the value by each key
with getItem
.
Conclusion
To loop through localStorage in HTML5 and JavaScript, we use Object.keys
and forEach
.