DEV Community

Cover image for How to save arrays and objects to localStorage using JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on β€’ Originally published at melvingeorge.me

How to save arrays and objects to localStorage using JavaScript?

Originally posted here!

The localStorage API in browsers only supports adding information in a key:pair format and the key and the pair should be of type string thus native objects or arrays cannot be stored in the localStorage.

To save arrays or objects using the localStorage API in JavaScript, we need to first stringify the arrays or objects using the JSON.stringify() method, and when we need to retrieve the value we can use the JSON.parse() method.

Consider this object,

// an object
const John = {
  name: "John Doe",
  age: 23,
};
Enter fullscreen mode Exit fullscreen mode

To save this object to the localStorage First, we need to convert the John object into a JSON string. For that let's use the JSON.stringify() method and pass the object as an argument to the method.

It can be done like this,

// an object
const John = {
  name: "John Doe",
  age: 23,
};

// convert object to JSON string
// using JSON.stringify()
const jsonObj = JSON.stringify(John);
Enter fullscreen mode Exit fullscreen mode

Now let's use the setItem() method on the localStorage and pass the key name as the first argument to the method and the jsonObj as the second argument to the method like this,

// an object
const John = {
  name: "John Doe",
  age: 23,
};

// convert object to JSON string
// using JSON.stringify()
const jsonObj = JSON.stringify(John);

// save to localStorage
localStorage.setItem("John", jsonObj);
Enter fullscreen mode Exit fullscreen mode

Now to retrieve the value from the localStorage, we can use the getItem() method on the localStorage API and then convert the string to a valid object using the JSON.parse() method.

It can be done like this,

// an object
const John = {
  name: "John Doe",
  age: 23,
};

// convert object to JSON string
// using JSON.stringify()
const jsonObj = JSON.stringify(John);

// save to localStorage
localStorage.setItem("John", jsonObj);

// get the string
// from localStorage
const str = localStorage.getItem("John");

// convert string to valid object
const parsedObj = JSON.parse(str);

console.log(parsedObj);
Enter fullscreen mode Exit fullscreen mode

Great! 🌟 We have successfully stored and retrieved from the localStorage API.

See this example live in JSBin.

The process is the same for arrays too.

See this example using arrays,

// an array
const arr = [1, 2, 3, 4, 5];

// convert array to JSON string
// using JSON.stringify()
const jsonArr = JSON.stringify(arr);

// save to localStorage
localStorage.setItem("array", jsonArr);

// get the string
// from localStorage
const str = localStorage.getItem("array");

// convert string to valid object
const parsedArr = JSON.parse(str);

console.log(parsedArr);
Enter fullscreen mode Exit fullscreen mode

See this example live in JSBin.

Feel free to share if you found this useful πŸ˜ƒ.


Top comments (0)

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay