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

后端 未结 6 1422
难免孤独
难免孤独 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 12:03

    best practice for checking if a variable is defined and not null:

    if (typeof sideBar !== 'undefined' && sideBar !== null)
    

    edited realized you're not checking if something is undefined, you're checking that it's defined, so edited again to answer your request more accurately

提交回复
热议问题