But before that, do you know what is localStorage?
localStorage allows developers to store and retrieve data in the browser. The data is stored in the form of “key”: “value” pairs & will not expire — which means that the data will be present even if the tab or the browser window is closed.
Remember — that the localStorage only stores strings in the form of “key”: “value” pairs. That brings us to the main question — how do you store arrays & objects?
– First, we need to stringify the arrays or objects using the JSON.stringify() method, and
– Secondly, when we need to retrieve the value we can use the JSON.parse() method
But why the hassle to stringify and parse the data?
It is simple – since localStorage can only store “strings” — so, before you store the array in the localStorage, you need to convert it to a string. And…
When you retrieve the array from the localStorage, because you will get a string — you’ll need to convert it to an array if you want to manipulate it. Isn’t it?