is this method of saving numbers or booleans in a simple offline web game okay or should I stop?

to save numbers and booleans I use array destructuring. I simply save by
localStorage.save=JSON.stringify([stat1, stat2, stat3]);
then I load by
[stat1, stat2, stat3] = JSON.parse(localStorage.save);
I just want to know if this is bad, and I should stop or if it is okay.
by the way strings aren’t included in this method unless you do ‘"’+stringvar+’"’ in the save code which is the only thing i noticed

>Solution :

Saving data to the local storage is fine (but keep in mind most browsers have a quota, but a few numbers certainly won’t hit that limit) but your localStorage.save = ... doesn’t make any sense and doesn’t save anything to the local storage. It just sets a custom property of the localStorage object. This may work as long as you don’t reload your site (as everything is kept in memory). But once you reload your site, your data is gone.

The correct way would be

localStorage.setItem("gameStats", JSON.stringify([stat1, stat2, stat3]))

and

let [stat1, stat2, stat3] = JSON.parse(localStorage.getItem("gameStats"));

Leave a Reply