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

SessionStorage setting variable boolean as NaN or null (Javascript)

when i set an item in session storage, (then refresh the page) and then set a variable as the item, for some reason it sets that variable as either NaN or null, how can i stop this? (save files)

    var myVar = false;

function save() {
    sessionStorage.setItem("myVar", myVar)
}

function load(){
    myVar = sessionStorage.getItem("myVar")
    myVar = parseInt(myVar) //is supposed to set myVar as the stored variable called "myVar"
    //if i do console.log() and print the variable value, it either shows up as NaN or Null
    //if i didnt do 'parseInt' then the value will be 'false', but the value is in strings???
    //(when value is in strings, it wont work in an "if (myVar == false){}")
}

>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

Values stored in sessionStorage are strings.

Use this to convert to boolean :

myVar = sessionStorage.getItem("myVar") === "true";

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