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 | Show 8 more comments2 Answers
Reset to default 20Per 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
版权声明:本文标题:javascript - sessionStorage - set Item to null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738656085a2105149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"null"
ornull
? – James Thorpe Commented Jun 29, 2016 at 13:30typeof sessionStorage.getItem("Foo")
say? – JJJ Commented Jun 29, 2016 at 13:32"null"
in there at some point then, rather than it being an actualnull
value. They're not the same thing – James Thorpe Commented Jun 29, 2016 at 13:35sessionStorage.removeItem("Foo")
– Marcos Pérez Gude Commented Jun 29, 2016 at 13:38setItem
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