Take the 2-minute tour ×
var DEST_VALUE = 1


How can i check whether the variable holds some value or not. When i do this, it does not work...

share|improve this question
This will work as expected - neither if condition will evaluate to true so the inner code will never be hit. What are you trying to achieve? –  Chris Francis Jun 28 '11 at 10:37
if(!DEST_VALUE) is same as if(DEST_VALUE == null), and from the code this will always be false. So it will not produce any result. –  Talha Ahmed Khan Jun 28 '11 at 10:40
possible duplicate of How can I test whether a variable has a value in JavaScript? –  Felix Kling Jun 28 '11 at 10:42
@Talha Not strictly true - if DEST_VALUE was 0 it would also evaluate to true. –  Chris Francis Jun 28 '11 at 10:43
@Talha: "if(!DEST_VALUE) is same as if(DEST_VALUE == null)" No, it doesn't. For instance, !0 is true but 0 == null is false. More to the point, though, if (!DEST_VALUE) has nothing to do with null unless DEST_VALUE happens to be null (in which case, the expression is true). It's about coercing the given value to a boolean, not comparing it to null. –  T.J. Crowder Jun 28 '11 at 10:45
show 1 more comment

3 Answers

up vote 4 down vote accepted

Of course it doesn't do anything, because in your example DEST_VALUE resolves to true like APPDAYS_AFTER. Value that resolve to false when converted to a boolean in javascript are:

The empty string ''
The number 0
The number NaN (yep, 'Not a Number' is a number, it is a special number)

if you write

    txtSiteId.value = fileContents.Settings.SiteID;

you write "if DEST_VALUE is not true do something" (in your case it does nothing). If you want to check if a variables hold a value:

if(DEST_VALUE !== undefined){
     //do something
share|improve this answer
This will throw an ReferenceError as you may not reference variables that are not declared in JS. See my answer for a solution. –  Steffen Müller Jun 28 '11 at 10:43
@Steffen: The question clearly shows the variables being declared. –  T.J. Crowder Jun 28 '11 at 10:47
But what about FileNotFound??? thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx –  cwallenpoole Jun 28 '11 at 10:52
add comment

I use such function to check if variable is empty or not:

function empty( mixed_var ) {
    return ( typeof(mixed_var) === 'undefined' || mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false );
share|improve this answer
Why would you do all this? !myVar checks for 'falsy' values, not just explicit false (such as myVar === false), so you don't need a bloated function to do the same as the native language capabilities. –  Chris Francis Jun 28 '11 at 10:39
By the way, a string representation of "0" shouldn't evaluate to false, only an empty string... –  Chris Francis Jun 28 '11 at 10:40
@Chris: Well, it's his function, he can do that if it matches his definition of "empty". It doesn't match mine, but... :-) –  T.J. Crowder Jun 28 '11 at 10:48
Yes - that function was changed several times and you can see the final result. Why !myVar is not used - because all is not so simple. I had some issues when it worked not as I expected too. –  Andron Jun 29 '11 at 21:33
add comment

I assume you mean "holds some value" like in "the variable has been created so it exists", right? Otherwise, your approach works perfectly fine.

If you want to check whether a variable exists in javascript, you have to check its parent object for the property - otherwise the script will fail. Each object in javascript belongs to a parent object, even if it seems to be global (then, it belongs to the window object). So, try something like:

  if (window.DEST_VALUE) 
    // do something
share|improve this answer
add comment

Your Answer


