I want to save an array in localStorage, what should I do?

Asked 2 years ago, Updated 2 years ago, 46 views

var names=new Array(); 
names[0]=prompt("New member name?");

You can just do this, but I have to save it to localStorage now.

var localStorage[names]=new Array();
localStorage.names[0]=prompt("New member name?");

You can't do it like this. What's wrong?

javascript html array html5

2022-09-22 08:15

1 Answers

LocalStorage supports only strings. Try using JSON.stringify() and JSON.parse().

var names = [];
names[0] = prompt("New member name?");
localStorage["names"] = JSON.stringify(names);

//...
var storedNames = JSON.parse(localStorage["names"]);

Like this


2022-09-22 08:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.