tags:

views:

141

answers:

5

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)
+5  A: 

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.

Andrzej Doyle
Null and Undefined types are == (but not ===).
rahul
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Comparison_Operators
rahul
Thanks for the link adamantium, added to my answer.
Andrzej Doyle
I want to check both scenarios, when the variable is null, and when there is no variable.
David García González
+1  A: 

I prefer

if (null == yourvar)

which avoids the accidental assignment in this scenario

if (yourvar = null)

Edit

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

* Two strings are strictly equal when they have the same sequence of characters, 
  same length, and same characters in corresponding positions.
* Two numbers are strictly equal when they are numerically equal (have the 
  same number value). NaN is not equal to anything, including NaN. 
  Positive and negative zeros are equal to one another.
* Two Boolean operands are strictly equal if both are true or both are false.
* Two objects are strictly equal if they refer to the same Object.

Null and Undefined types are == (but not ===)

Read Comparison Operators

rahul
Any reason for the down vote?
rahul
@adamantium: please edit. You added a lot since my downvote ;) Originally you completely missed the goal of the question (avoiding `undefined`), which now you've addressed.
Crescent Fresh
A: 

If you don't care if it's precisely null or undefined or false or 0, and just want to see if it's basically "unset", don't use an operator at all:

if (yourVar)
Cory Petosky
The only problem with that, is that if `yourVar` is set to 0, or '0', or an empty string, or possibly a whole bunch of other things then it will still return `false`. So that's a very bad way to check if it's been set in general; only applicable if you **know** all possible values are not *falsy*.
Andrzej Doyle
Yes. But since he mentions comparing to both null and undefined up top, I'm assuming he's doing something _objecty_ and won't really have to worry about those issues.
Cory Petosky
+1  A: 

"undefined" is not "null". Compare

  • the spoon is empty (=null)
  • there is no spoon (=undefined)

some facts that can help you further

  • typeof undefined is "undefined"
  • typeof null is "object"
  • undefined is considered equal (==) to null and vice versa
  • no other value is equal (==) to either null or undefined
stereofrog
A: 

if (null == yourvar) and if (typeof yourvar != 'undefined') do very different things. One assumes the variable exists, the other does. I would suggest not to mix the two at all. Know when to expect the variable deal with it's presence before you deal with its value.

Peter Bengtsson