Categories
JavaScript Answers

How to loop through localStorage in HTML5 and JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *