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?
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 )
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With