How can I check if a localstorage variable is null or undefined?

后端 未结 6 1423
难免孤独
难免孤独 2021-02-05 11:35

I have this code:

var sideBar = localStorage.getItem(\'Sidebar\');

I want to check if sideBar is defined and not null in an if statement. I am

6条回答
  •  情歌与酒
    2021-02-05 11:59

    This should work, and no there isn't a way outside of an "if", even if it ternary operator,

    if( !value ) {
    }
    

    This will check if there value is "truethy" and should cover both "null" and "undefined".

提交回复
热议问题