Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to figure out what the setItem method from sessionStorage returns. As far as I could get, the following code returns undefined:

var set = sessionStorage.setItem('foo', 'bar');
console.log(set);

I need to know if the item was successfully set or if it failed. How can I accomplish this without knowing the return?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
687 views
Welcome To Ask or Share your Answers For Others

1 Answer

Take a look at the sessionStorage specification.

This line:

setter creator void setItem(DOMString key, DOMString value);

Tells us setItem doesn't return anything. (void is the return value, there)


You can check if the item was set like this:

if (sessionStorage.getItem('myValue') == null){
    // myValue was not set
}else{
    // myValue was set
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...