Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading