Sometimes, we want to retrieve all localStorage items without knowing the keys in advance with JavaScript.
In this article, we’ll look at how to retrieve all localStorage items without knowing the keys in advance with JavaScript.
How to retrieve all localStorage items without knowing the keys in advance with JavaScript?
To retrieve all localStorage items without knowing the keys in advance with JavaScript, we can make a copy of the localStorage
object.
For instance, we write
const items = { ...localStorage };
to use the spread operator with localStorage
to make a shallow copy of the localStorage
object and assign it to items
.
Conclusion
To retrieve all localStorage items without knowing the keys in advance with JavaScript, we can make a copy of the localStorage
object.