How to save arrays and objects to localStorage using JavaScript?

Nirmal Joshi
2 min readMay 23, 2022

--

In this blog, I’ll be discussing a few pointers on how to save arrays and objects to localStorage using JavaScript.

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?

Well, to save arrays or objects with the help of localStorage API using JavaScript, there are 2 methods:

- 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?

Check out this video on my YouTube Channel to know more about how to save arrays and objects to localStorage using JavaScript. I have explained this concept with the help of a simple example.

Check out my bestselling and highest rated Udemy course —
JavaScript — Marathon Interview Questions Series

Looking for remote/contract Web/UI/Full stack opportunities in the US? Connect with the leading staffing experts of the industry today.

About me:

Founder and CEO of an IT company in India, I have more than 25 years experience of in dealing with people, processes, and codes. I started online training for my students when it was not in fashion and have trained more than 1000 students/working professionals personally which has helped them to secure awesome jobs or even start their own businesses.

Check out my Udemy profile to know more about the courses that I teach.

I am also been an active corporate trainer for several years now and have been consulting with top Fortune 500/1000 companies to streamline their development projects efficiently. My goal is to share knowledge with a primary focus on advanced tools & techniques, projects, and standard programming practices to help my students understand the basics and fundamentals and make awesome technological implementations.

--

--

Nirmal Joshi
Nirmal Joshi

Written by Nirmal Joshi

A founder and CEO of an IT company in India, I have more than 22+ years’ experience of dealing with people, processes and codes.

No responses yet