8

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)
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
David García González
  • 5,164
  • 8
  • 29
  • 35

5 Answers5

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.

Community
  • 1
  • 1
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
4

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
  • 184,426
  • 49
  • 232
  • 263
  • @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 Nov 13 '09 at 10:58
1

"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
user187291
  • 53,363
  • 19
  • 95
  • 127
0

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
  • 12,458
  • 3
  • 39
  • 44
  • 1
    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 Nov 13 '09 at 10:39
  • 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 Nov 13 '09 at 11:17
0

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
  • 7,235
  • 8
  • 44
  • 53