Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a localStorage string to an Integer

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;
like image 877
qwerty77asdf Avatar asked Apr 28 '26 16:04

qwerty77asdf


2 Answers

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()
like image 57
Tiago Fabre Avatar answered May 01 '26 06:05

Tiago Fabre


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"
like image 23
Ali Mamedov Avatar answered May 01 '26 05:05

Ali Mamedov