1

My code:

var old = localStorage.getItem('GotoBeginning');
console.log("old is "+ old);
if (old===true) {
  console.log("Returning true");
  $("#gobeg").prop('checked', true);
  return true;
} else {
  console.log("Returning false");
  $("#gobeg").prop('checked', false);
  return false;
}

The value in localStorage for GotoBeginning is true. My console.log shows:

old is true
Returning false

I expected the following output:

old is true
Returning true
0

1 Answer 1

2

The storage api in the browser only stores strings. This should work:

if (old === 'true') {
//          ^    ^

} else {

}

As mentioned in a comment by IrkenInvader, you can extract the proper type like so:

var old = JSON.parse(localStorage.getItem('GotoBeginning'))
// now old will be a proper boolean so this will work
if (old) {

} else {

}
Sign up to request clarification or add additional context in comments.

2 Comments

or use JSON.parse(localStorage.getItem('GotoBeginning'));
And also try to: console.log('retrievedObject: ', JSON.parse(old));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.