-1

如果我的变量已定义,我需要设置一个值,而不是 null 并且等于 "" 或其他任何值。

我正在使用以下内容:

if (typeof abc != 'undefined') {
   if (abc != null) {
      // Here the variable abc must be defined
      // not null
      // equal to "" or anything else such as a longer string or number
   }   
}

有人可以告诉我这是否是我实施所需检查的最佳方式。

4

2 回答 2

0
var abc2 = abc.toString();

if (abc2.length > 0)

如果长度> 0,则设置为

于 2013-10-18T09:40:32.057 回答
0

Javascript 将任何这些情况验证为布尔值,因此:

if ( abc ) {
    ....
}

检查所有这些情况。

于 2013-10-18T10:14:40.080 回答