views:

73

answers:

3

I want to run a code only if the argument[0].recordCount is greater than zero or is NOT undefined. However, the code is ran when the argument[0].recordCound alert shows undefined.

if(arguments[0].recordCount > 0 && 
   arguments[0].recordCount !== 'undefined')
{ //if more than 0 records show in bar

    document.getElementById('totalRecords').innerHTML = 
       arguments[0].recordCount + " Records";
}

How can I test for undefined here?

+2  A: 

undefined is a keyword a global variable with constant value, use it without quotes:

if(arguments[0].recordCount > 0 && arguments[0].recordCount !== undefined)

But actually it would be sufficient to test only the first condition:

if(arguments[0].recordCount > 0)

because if recordCount is larger than zero, it is defined anyway.


More common is to switch the conditions and test first whether it is defined, to avoid possible errors in the following tests (not sure if this is necessary here):

if(arguments[0].recordCount !== undefined && arguments[0].recordCount > 0)
Felix Kling
Right, if you insist in comparing it as a `'string'`, use `!=` instead of `!==`. The former just tests the value, while the latter tests **both** the type and value.
BalusC
`undefined` is *not* a keyword in JavaScript. However, `typeof(x)` will return the string `"undefined"` if `x` is undefined.
stakx
@stakx: Thank you for clarifying.
Felix Kling
`undefined` can be changed. you shouldn't be comparing against it. if `recordCount` is not guaranteed to have some value when you do the comparison, it's going to blow up.
lincolnk
+5  A: 

When using undefined as a string you need to do so with the typeof operator.

Also, you should be checking if it's defined before any other checks on the property.

if ( 'undefined' != typeof arguments[0].recordCount && arguments[0].recordCount > 0 )
Peter Bailey
+1. Probably the `!==` operator should be used instead of `!=` ?
stakx
stakx: no. `typeof` is guaranteed to return a string, and you're comparing its return value to another string, so it'll always be fine, no matter what Crockfordian habits you may have learned :)
Tim Down
@stakx - irrelevent. `typeof` returns a string.
Peter Bailey
@Tim: You're right. It's become quite a persistent habit, whenever I see a `==` or `!=` I think it should be the strict operators instead.
stakx
+1  A: 

To check for a variable to be not null and not undefined,

if(thatVariable) is enough though implicit conversion can cause problem for some cases where thatVariable is empty string, or a boolean, or number 0. If implicit conversion is not the case for our variable, the following would do,

if(arguments[0].recordCount && arguments[0].recordCount > 0)

But the following would be problematic,

if(arguments[0].recordCount !== undefined && arguments[0].recordCount > 0)

Consider,

var undefined = 'surprise' //possible since undefined is not a keyword
if(arguments[0].recordCount !== undefined && arguments[0].recordCount > 0)

Now this 'if' will break even though recordCount is undefined.

One more thing: if(a != null) will also check undefined due to implicit conversion. Hence if(a != null && a != undefined) is redundant

Marimuthu Madasamy