I have some values that are dynamically stored at localStorage with incremented values like this: localStorage[“Value0”], localStorage[“Value1”],….
When I try to access them like this:
JavaScript:
localStorage["Counter"]=0;
var i = localStorage["Counter"];
var d =localStorage["Value"+i];
i = i + 1; // i becomes "01"
var f = localStorage["Value"+i];
The i’s value is “01” not 1 … Is there a way to increment the i’s value correctly?
LocalStorage can only store string values. You can use parseInt which converts a string into an integer:
You can also use libraries like store.js to do things automatically for you. All you have to do is to include the library:
Set a new storage:
Put things into it:
Get the value and do anything you want with it:
And you can also go crazy and use some arrays:
And change things inside it:
No type conversion needed. You can also store objects, booleans and other stuff. Just don’t forget to save things back in the storage since it doesn’t automatically do it.