Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  109] [ 7]  / answers: 1 / hits: 15082  / 15 Years ago, fri, november 13, 2009, 12:00:00

What is the mos reliable way if I want to check if the variable is null or is not present?.



There are diferent examples:



if (null == yourvar)

if (typeof yourvar != 'undefined')

if (undefined != yourvar)

More From » javascript

 Answers
13

None of the above.



You don't want to use ==, or a variety thereof, because it performs type coercion. If you really want to check whether something is explicitly null, use the === operator.



Then again, your question shows perhaps some lack of clarity of your requirements. Do you actually mean null specifically; or does undefined count too? myVar === null will certainly tell you if the variable is null, which is the question you asked, but is this really what you want?



Note that there's a lot more information in this SO question. It's not a direct duplicate, but it covers very similar principles.


[#98322] Monday, November 9, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaylinshayd

Total Points: 443
Total Questions: 104
Total Answers: 111

Location: Nauru
Member since Wed, Mar 29, 2023
1 Year ago
;