I am trying to make a save function for my game and, it isn't allowing me to save any of my Variables as integers (even with parseInt(Variable), +Variable, etc.)
The answers to this post aren't working.
localStorage.value = value;
value = localStorage.value;
Try to use parseInt(localstorage.numericProperty)
function populateStorage() {
let obj = {x: "sdfsd", y: "sdfsdf"};
localStorage.setItem("image", JSON.stringify(obj));
localStorage.bgcolor = "blue";
localStorage.numeric = 3;
}
function checkStorage(){
console.log(localStorage.getItem("image"));
console.log(localStorage.bgcolor);
console.log(parseInt(localStorage.numeric) + 1);
}
populateStorage();
checkStorage()
The best way to convert any number data in localStorage from string to number:
var a = localStorage['some_property'];
typeof a; // "string"
var b = +localStorage['some_property'];
typeof b; // "number"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With