如何在JavaScript中检查空值?我写了下面的代码,但没用。
if (pass == null || cpass == null || email == null || cemail == null || user == null) {
alert("fill all columns");
return false;
}
如何在JavaScript程序中查找错误?
如何在JavaScript中检查空值?我写了下面的代码,但没用。
if (pass == null || cpass == null || email == null || cemail == null || user == null) {
alert("fill all columns");
return false;
}
如何在JavaScript程序中查找错误?
当前回答
首先,您有一个没有函数体的return语句。这很可能会引发错误。
一个更干净的检查方法是简单地使用!操作员:
if (!pass || !cpass || !email || !cemail || !user) {
alert("fill all columns");
}
其他回答
要检查空值,请使用以下命令:
if (variable === null)
此测试仅通过null,不通过“”、undefined、false、0或NaN。
此外,我还为每个“类假”值提供了绝对检查(对于!变量返回true)。
注意,对于某些绝对检查,您需要使用绝对等于:==和typeof。
我在这里创建了一个JSFiddle,以显示所有单独测试的工作情况
以下是每次检查的输出:
Null Test:
if (variable === null)
- 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 === '')
- 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 (typeof variable == "undefined")
-- or --
if (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)
- 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)
- 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 (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)
-- or --
if (isNaN(variable))
- 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有点困难;
检查错误条件:
// Typical API response data
let data = {
status: true,
user: [],
total: 0,
activity: {sports: 1}
}
// A flag that checks whether all conditions were met or not
var passed = true;
// Boolean check
if (data['status'] === undefined || data['status'] == false){
console.log("Undefined / no `status` data");
passed = false;
}
// Array/dict check
if (data['user'] === undefined || !data['user'].length){
console.log("Undefined / no `user` data");
passed = false;
}
// Checking a key in a dictionary
if (data['activity'] === undefined || data['activity']['time'] === undefined){
console.log("Undefined / no `time` data");
passed = false;
}
// Other values check
if (data['total'] === undefined || !data['total']){
console.log("Undefined / no `total` data");
passed = false;
}
// Passed all tests?
if (passed){
console.log("Passed all tests");
}
严格相等运算符:-
我们可以通过===
if ( value === null ){
}
仅通过使用if
if( value ) {
}
如果值不是:
无效的未定义NaN公司空字符串(“”)假的0
JavaScript在检查“空”值方面非常灵活。我猜你实际上是在寻找空字符串,在这种情况下,这个更简单的代码会起作用:
if(!pass || !cpass || !email || !cemail || !user){
它将检查空字符串(“”)、null、undefined、false以及数字0和NaN。
请注意,如果您是专门检查数字,则使用此方法错过0是一个常见错误,num!==对于返回-1的函数(例如indexOf),首选0(或num!==-1或~num(同时检查-1的黑客代码))。
您可以检查某些值是否为空,如下所示
[pass,cpass,email,cemail,user].some(x=> x===null)
let pass=1;设cpass=2;让电子邮件=3;let cemail=空;让用户=5;如果([pass,cpass,email,cemail,user].some(x=>x==null)){警报(“填充所有列”);//return false;}
奖金:为什么==比==更清楚(来源)
a==b
a===b