41

Possible Duplicate:
Detecting an undefined object property in JavaScript
How to determine if variable is 'undefined' or 'null'
Is there a standard function to check for null, undefined, or blank variables in JavaScript?

In my code, I have a condition that looks like

if (variable !== null && variable !== undefined) {
}

But instead of doing it in two steps, i.e checking if it is not defined and not null. Is there a one step checking that replaces this check.

GSangram
  • 123
  • 10
PMat
  • 2,039
  • 2
  • 29
  • 46
  • It's not exactly slowing down your code, if that's what you're worried about. – Blazemonger Sep 04 '12 at 21:44
  • Use a function: `function isNullOrUndefined(variable) { return variable === null || variable === undefined; }`. – VisioN Sep 04 '12 at 21:46
  • @VisioN If you pass an undefined variable to a function as an argument, you will get an error. – dqhendricks Sep 04 '12 at 21:48
  • @dqhendricks that's not true, I don't think, though of course it may cause an error *inside* the function. – Pointy Sep 04 '12 at 21:49
  • @VisioN you can certainly write a function but the `!=` operator handles this case already. – Pointy Sep 04 '12 at 21:50
  • @Pointy Well yes. Just that insane idea... – VisioN Sep 04 '12 at 21:51
  • @Pointy it most certainly is true. Run this and see what pops up in your log... http://jsfiddle.net/8JfbT/ – dqhendricks Sep 04 '12 at 21:51
  • @dqhendricks But it won't be an error if you set `baz` as `undefined`. Hence, it works only if the code is correct a priori. – VisioN Sep 04 '12 at 21:54
  • @dqhendricks ah well there's a difference between an "undefined variable" and a "variable with the **value** `undefined`" ... [here's a modified version of your fiddle](http://jsfiddle.net/Pointy/8JfbT/1/) – Pointy Sep 04 '12 at 21:59
  • Yes, @Pointy My point is still valid however. If you want to test whether a variable has been defined, you cannot use a function. – dqhendricks Sep 04 '12 at 22:46
  • @dqhendricks—any attempt to read an undeclared or un–initialsed variable will throw an error (except for `typeof`), including in the OP. – RobG Sep 04 '12 at 23:04
  • @RobG yep. `if (typeof foo === 'undefined') {` – dqhendricks Sep 04 '12 at 23:10
  • the best way to do this is if (!!!variable) { //do something } – msroot Apr 23 '15 at 03:24
  • @msroot that test will also succeed if the value of the variable is `0`, `NaN`, the empty string, or `false`. – Pointy Feb 09 '16 at 22:07
  • if (variable) { // your content here } will do the magic. In Js both `null` and `undefined` returns `false` inside if condition. – GSangram Feb 19 '20 at 13:44

2 Answers2

64

A variable cannot be both null and undefined at the same time. However, the direct answer to your question is:

if (variable != null)

One =, not two.

There are two special clauses in the "abstract equality comparison algorithm" in the JavaScript spec devoted to the case of one operand being null and the other being undefined, and the result is true for == and false for !=. Thus if the value of the variable is undefined, it's not != null, and if it's not null, it's obviously not != null.

Now, the case of an identifier not being defined at all, either as a var or let, as a function parameter, or as a property of the global context is different. A reference to such an identifier is treated as an error at runtime. You could attempt a reference and catch the error:

var isDefined = false;
try {
  (variable);
  isDefined = true;
}
catch (x) {}

I would personally consider that a questionable practice however. For global symbols that may or may not be there based on the presence or absence of some other library, or some similar situation, you can test for a window property (in browser JavaScript):

var isJqueryAvailable = window.jQuery != null;

or

var isJqueryAvailable = "jQuery" in window;
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • While this is correct and answers the OP's question, I think the original code is clearer. Not everyone will recognise the subtle difference between `x != null` and say `!x`. – RobG Sep 04 '12 at 22:59
  • 1
    @RobG well, I generally don't give "everyone" access to my source code repository :-) I think it's kind-of important for JavaScript coders to know things like this, in other words. – Pointy Sep 04 '12 at 23:03
  • Brilliant answer and it should have been accepted as the best. – toni rmc May 03 '15 at 17:05
  • it's not working for undefined variable – Parth Chavda May 18 '16 at 11:31
  • @ParthChavda what exactly do you mean? What's not working? In what way? – Pointy May 18 '16 at 12:53
  • @Pointy when variable undefined – Parth Chavda May 18 '16 at 12:55
  • 1
    @ParthChavda As noted in the comments to the original question, testing for a variable simply not existing at all is not the intention of this code. That situation - the use of a non-defined variable - is simply an error, and is flagged as such in "strict" mode. – Pointy May 18 '16 at 13:01
  • @Pointy thank you,i got it. – Parth Chavda May 18 '16 at 13:04
7

You can wrap it in your own function:

function isNullAndUndef(variable) {

    return (variable !== null && variable !== undefined);
}
KDaker
  • 5,899
  • 5
  • 31
  • 44
  • 2
    Or atleast `return (variable !== null && variable !== undefined);` Come on! Thats like saying `if(true) { return true; } return false;` – jeremy Sep 05 '12 at 03:12