最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom events - Testing for undefined in JavaScript - Stack Overflow

programmeradmin8浏览0评论

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?

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?

Share Improve this question edited Jul 25, 2020 at 8:28 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 22, 2010 at 18:11 Asim ZaidiAsim Zaidi 28.4k49 gold badges138 silver badges224 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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 mon 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)

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

发布评论

评论列表(0)

  1. 暂无评论