BLOG POST

Getting an Array from Local Storage with Javascript




The local storage feature in HTML gives you a powerful alternative to the traditional cookie. This option lets you store complex data locally on the user's browser. While retrieving a string value can be easy, getting a more complex structure, like an array requires a bit more.

Retrieving an Array from Local Storage

If you save an array to local storage the way Ecofic recommends, it will be stored as a string. To get it back into a usable form, you can use the complementary function JSON.parse. A sample is shown here: Its important to recognize that "getItem" will return null if the key isn't in storage. Because of that, you may want to add an additional check. Regardless, the process of using local storage involves serializing / deserializing. This process is leveraged via the JSON.stringify and JSON.parse methods.‚Äč