如何检查JavaScript中的空值?
我如何检查JavaScript中的空值? 我写了下面的代码,但没有奏效。
if (pass == null || cpass == null || email == null || cemail == null || user == null) {
alert("fill all columns");
return false;
}
我怎样才能在我的JavaScript程序中发现错误?
Javascript在检查“空值”方面非常灵活。 我猜你实际上是在寻找空字符串,在这种情况下,这个简单的代码将工作:
if(!pass || !cpass || !email || !cemail || !user){
它将检查空字符串( ""
), null
, undefined
, false
以及数字0
和NaN
请注意,如果您正在专门检查数字,则使用此方法错过0
是常见的错误,并且num !== 0
是首选(或num !== -1
或~num
(hacky代码也会针对-1
))返回-1
函数,例如indexOf
)
要检查空具体来说 ,你这样做:
if(variable === null && typeof variable === "object")
...或者更简单:
if(variable === null)
本次测试将只通过为null
,也不会传为""
, undefined
, false
, 0
,或NaN
。
其余部分是为了回应无政府主义者的评论,是的,你可以单独检查每一个。
您需要实现absolutely equals: ===
和typeof
以绝对确保您的支票。
我在这里创建了一个JSFiddle来显示所有单独的测试工作
这里是测试的所有输出:
Null Test:
if(variable === null && typeof variable === "object")
- variable = ""; (false) typeof variable = string
- variable = null; (true) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Empty String Test:
if(variable === "" && typeof variable === "string")
- variable = ""; (true) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Undefined Test:
if(variable === undefined && typeof variable === "undefined")
- variable = ""; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (true) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
False Test:
if(variable === false && typeof variable === "boolean")
- variable = ""; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (true) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Zero Test:
if(variable === 0 && typeof variable === "number")
- variable = ""; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (true) typeof variable = number
- variable = NaN; (false) typeof variable = number
NaN Test:
if(!parseFloat(variable) && variable != 0 && typeof variable === "number")
- variable = ""; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (true) typeof variable = number
正如你所看到的,对NaN
进行测试有点难度。
只需在所有地方用===
替换==
。
==
是一个宽松或抽象的平等比较
===
是一个严格的平等比较
有关更多详细信息,请参阅MDN有关平等比较和相同性的文章。
链接地址: http://www.djcxy.com/p/76651.html