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

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

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

}   

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


当前回答

这是对WebWander关于检查NaN的解决方案的评论(我还没有足够的代表留下正式评论)。解决方案如下

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

但对于将舍入为0的有理数(例如变量=0.1),这将失败。更好的测试是:

if(isNaN(variable) && typeof variable === "number")

其他回答

我做了一个非常简单的功能,效果很好:

function safeOrZero(route) {
  try {
    Function(`return (${route})`)();
  } catch (error) {
    return 0;
  }
  return Function(`return (${route})`)();
}

路线是任何一条价值链都会爆炸。我将它用于jQuery/cacherio和对象等。

示例1:一个简单的对象,例如this const testObj={items:[{val:'haya'},{val:null},{val:'hum!'}];};。

但它可能是一个非常大的物体,我们甚至没有制造过。所以我通过了:

let value1 = testobj.items[2].val;  // "hum!"
let value2 = testobj.items[3].val;  // Uncaught TypeError: Cannot read property 'val' of undefined

let svalue1 = safeOrZero(`testobj.items[2].val`)  // "hum!"
let svalue2 = safeOrZero(`testobj.items[3].val`)  // 0

当然,如果您愿意,可以使用null或“无值”。。。任何适合您需要的。

通常,如果找不到DOM查询或jQuery选择器,可能会抛出错误。但使用类似于:

const bookLink = safeOrZero($('span.guidebook > a')[0].href);
if(bookLink){
  [...]
}

你可以使用try-catch finally

 try {
     document.getElementById("mydiv").innerHTML = 'Success' //assuming "mydiv" is undefined
 } catch (e) {

     if (e.name.toString() == "TypeError") //evals to true in this case
     //do something

 } finally {}   

您也可以抛出自己的错误。看看这个。

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

当pass为空字符串时,您可能希望pass==null为真,因为您知道松散的相等运算符==执行某些类型的强制。

例如,此表达式为true:

'' == 0

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

'' === 0

假定“”和0大致相等,您可以合理地推测“”和null大致相等。然而,事实并非如此。

此表达式为false:

'' == null

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

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

pass === ''

如果您确定pass是一个字符串,pass==“”也会起作用,因为只有空字符串与空字符串大致相等。另一方面,一些专家表示,在JavaScript中始终使用严格相等是一种好的做法,除非您特别想执行松散相等运算符执行的类型强制。

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

严格相等运算符:-

我们可以通过===

if ( value === null ){

}

仅通过使用if

if( value ) {

}

如果值不是:

无效的未定义NaN公司空字符串(“”)假的0

JAVASCRIPT中的AFAIK当变量已声明但未赋值时,其类型未定义。所以我们可以检查变量,即使它是一个持有某个实例代替值的对象。

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

检查变量是否为空的helper函数:

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