Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  199] [ 4]  / answers: 1 / hits: 55365  / 12 Years ago, thu, november 15, 2012, 12:00:00

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 a bit confused I know there's a: sideBar == undefined and sideBar != null



but is there a good way for me to do this test for both of these inside an if:



if (??) 

More From » javascript

 Answers
8

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


[#81983] Tuesday, November 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antonb

Total Points: 424
Total Questions: 104
Total Answers: 101

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;