如何在JavaScript中检查空值?

JavaScript

飞云卡卡西神乐

2020-03-12

如何在JavaScript中检查空值?我在下面编写了代码,但是没有用。

if (pass == null || cpass == null || email == null || cemail == null || user == null) {      

    alert("fill all columns");
    return false;  

}   

在JavaScript程序中如何找到错误?

第1062篇《如何在JavaScript中检查空值?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

12个回答
神无神无 2020.03.12

要检查javascript中的undefinednull,只需编写以下代码:

if (!var) {
        console.log("var IS null or undefined");
} else {
        console.log("var is NOT null or undefined");
}
DavaidAPro 2020.03.12

严格等于运算符:

我们可以通过检查null ===

if ( value === null ){

}

只需使用 if

if( value ) {

}

如果值不是,将评估为true

  • 空值
  • 未定义
  • N
  • 空字符串(“”)
  • 0
逆天米亚 2020.03.12

声明变量但未分配值的JAVASCRIPT中的 AFAIK ,其类型为所以我们可以检查变量,即使这将是一个持有一些实例到位的价值undefinedobject

创建一个用于检查返回的无效性的辅助方法,true并在您的API中使用它。

帮助程序函数,以检查变量是否为空:

function isEmpty(item){
    if(item){
        return false;
    }else{
        return true;
    }
}

尝试捕获异常API调用:

try {

    var pass, cpass, email, cemail, user; // only declared but contains nothing.

    // parametrs checking
    if(isEmpty(pass) || isEmpty(cpass) || isEmpty(email) || isEmpty(cemail) || isEmpty(user)){
        console.log("One or More of these parameter contains no vlaue. [pass] and-or [cpass] and-or [email] and-or [cemail] and-or [user]");
    }else{
        // do stuff
    }

} catch (e) {
    if (e instanceof ReferenceError) {
        console.log(e.message); // debugging purpose
        return true;
    } else {
        console.log(e.message); // debugging purpose
        return true;
    }
}

一些测试用例:

var item = ""; // isEmpty? true
var item = " "; // isEmpty? false
var item; // isEmpty? true
var item = 0; // isEmpty? true
var item = 1; // isEmpty? false
var item = "AAAAA"; // isEmpty? false
var item = NaN; // isEmpty? true
var item = null; // isEmpty? true
var item = undefined; // isEmpty? true

console.log("isEmpty? "+isEmpty(item));
ItachiDavaid 2020.03.12

尝试这个:

if (!variable && typeof variable === "object") {
    // variable is null
}
阿飞米亚 2020.03.12

您可以使用lodash模块检查值是否为null或未定义

_.isNil(value)
Example 

 country= "Abc"
    _.isNil(country)
    //false

   state= null
    _.isNil(state)
    //true

city= undefined
    _.isNil(state)
    //true

   pin= true
    _.isNil(pin)
    // false   

参考链接:https : //lodash.com/docs/#isNil

神无Davaid 2020.03.12

对于来自DB for ex的布尔值,这将不起作用:

 value = false

 if(!value) {
   // it will change all false values to not available
   return "not available"
 }
神无Tom 2020.03.12

JavaScript在检查“空”值方面非常灵活。我猜您实际上是在寻找空字符串,在这种情况下,这种简单的代码将起作用:

if(!pass || !cpass || !email || !cemail || !user){

这将检查空字符串(""), ,null 以及数字undefinedfalse0NaN

请注意,如果您专门检查数字,则0使用此方法时常会犯错,对于返回的函数,它num !== 0是首选(num !== -1~num(也检查了错误代码-1)))-1,例如indexOf

西门达蒙 2020.03.12

通过显式检查null但使用简化的语法来改进可接受的答案

if ([pass, cpass, email, cemail, user].every(x=>x!==null)) {
    // your code here ...
}

// Test
let pass=1, cpass=1, email=1, cemail=1, user=1; // just to test

if ([pass, cpass, email, cemail, user].every(x=>x!==null)) {
    // your code here ...
    console.log ("Yayy! None of them are null");
} else {
    console.log ("Oops! At-lease one of them is null");
}

小小Pro 2020.03.12

在JavaScript中,没有字符串等于null

也许您希望pass == null当when pass是空字符串true,因为您知道松散的等价运算符==会执行某些类型的类型强制。

例如,此表达式为true:

'' == 0

相反,严格相等运算符===说这是错误的:

'' === 0

鉴于''0大致相等,您可以合理地猜想''null大致相等。但是,事实并非如此。

此表达式是错误的:

'' == null

将任何字符串与比较的结果null为false。因此,pass == null您的所有其他测试始终为假,并且用户永远不会收到警报。

要修复您的代码,请将每个值与空字符串进行比较:

pass === ''

如果您确定这pass是一个字符串,那么pass == ''也将起作用,因为只有一个空字符串大致等于该空字符串。另一方面,一些专家说,除非您特别想执行松散相等运算符执行的类型强制,否则始终在JavaScript中始终使用严格相等是一种好习惯。

如果您想知道哪些值对大致相等,请参阅关于此主题Mozilla文章中的“相似性比较”表

Green老丝Itachi 2020.03.12

这是对WebWanderer有关检查NaN的解决方案的评论(我没有足够的代表来发表正式评论)。解决方案读为

if(!parseInt(variable) && variable != 0 && typeof variable === "number")

但这对于四舍五入的有理数0(如)将失败variable = 0.1更好的测试是:

if(isNaN(variable) && typeof variable === "number")
阳光阿飞 2020.03.12

只需更换=====在所有地方。

== 是一个松散或抽象的相等比较

=== 是严格的平等比较

有关更多详细信息,请参见MDN上有关“ 平等比较和相同性”的文章

老丝猿路易 2020.03.12

首先,您有一个没有函数体的return语句。可能会引发错误。

一种更清洁的检查方法是简单地使用!操作员:

if (!pass || !cpass || !email || !cemail || !user) {

    alert("fill all columns");

}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android