Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable is both null and /or undefined in JavaScript [duplicate]

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.

like image 926
PMat Avatar asked Sep 04 '12 21:09

PMat


People also ask

How do you check if a variable is null or undefined in JavaScript?

The typeof operator for undefined value returns undefined . Hence, you can check the undefined value using typeof operator. Also, null values are checked using the === operator.

IS null === undefined JavaScript?

In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value. You can assign it to a variable.

Is null == undefined?

It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

Why JavaScript has both undefined and null?

The value null represents the intentional absence of any object value. It's never assigned by the runtime. Meanwhile any variable that has not been assigned a value is of type undefined . Methods, statements and functions can also return undefined .


2 Answers

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 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; 
like image 165
Pointy Avatar answered Oct 06 '22 04:10

Pointy


You can wrap it in your own function:

function isNullAndUndef(variable) {      return (variable !== null && variable !== undefined); } 
like image 20
KDaker Avatar answered Oct 06 '22 05:10

KDaker