admin管理员组

文章数量:1202815

I check if a sessionStorage Variable is null, and if true initialize it with "BAR". Otherwhise there should appear a console message with the value of the variable.

sessionStorage.setItem("Foo", null); //I know this would make no sense at this part, this is just here for demo.

if (sessionStorage.getItem("Foo") == null) {
  sessionStorage.setItem("Foo", "BAR");
} else {
  console.log("Foo != null (proof = '", sessionStorage.getItem("Foo"), "')");
}

I check if a sessionStorage Variable is null, and if true initialize it with "BAR". Otherwhise there should appear a console message with the value of the variable.

sessionStorage.setItem("Foo", null); //I know this would make no sense at this part, this is just here for demo.

if (sessionStorage.getItem("Foo") == null) {
  sessionStorage.setItem("Foo", "BAR");
} else {
  console.log("Foo != null (proof = '", sessionStorage.getItem("Foo"), "')");
}

However, It always goes to the else statement and i get the console message which is telling me that the variable is "null", but if it is really "null" then why it is not initialized with "BAR"?

Share Improve this question edited Dec 20, 2018 at 12:21 Black asked Jun 29, 2016 at 13:29 BlackBlack 20.2k45 gold badges185 silver badges295 bronze badges 13
  • 1 Is it telling you that it is "null" or null? – James Thorpe Commented Jun 29, 2016 at 13:30
  • 2 You can't tell by the code you have there. What does typeof sessionStorage.getItem("Foo") say? – JJJ Commented Jun 29, 2016 at 13:32
  • 1 It looks like you've stored the string "null" in there at some point then, rather than it being an actual null value. They're not the same thing – James Thorpe Commented Jun 29, 2016 at 13:35
  • 3 @EdwardBlack you can't make that. You can't store objects in sessionStorage. You can only store strings. So to remove something you need sessionStorage.removeItem("Foo") – Marcos Pérez Gude Commented Jun 29, 2016 at 13:38
  • 2 Because setItem requires a string value, anything that isn't will be coerced to a string. null becomes "null". – James Thorpe Commented Jun 29, 2016 at 13:38
 |  Show 8 more comments

2 Answers 2

Reset to default 20

Per your edit, setItem requires the value passed in to be a string (a DOMString to be precise), anything else will be coerced to a string.

In your case, you pass in null, which is coerced to "null". When you call getItem, you get a string back, which isn't null, so your code falls into the else block.

If you actually want to clear out an item from storage, the correct method to use is removeItem:

sessionStorage.removeItem("Foo");

Calling getItem now would result in a return value of null, not "null".

HTML5 storage: sessionStorage or localStorage accepts a string. If a string value is not passed it will convert it to string and save. Hence we should convert the value to string using (JSON.stringify(value)) and after fetching we should again convert it to the original value.

var val = null;
//save to storage
sessionStorage.setItem('a',JSON.stringify(val));

//fetch from storage
var stringValue = sessionStorage.getItem('a');  //outputs "null"
var originalValue = JSON.parse(sessionStorage.getItem('a')); //outputs null

本文标签: javascriptsessionStorageset Item to nullStack Overflow