如何在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程序中查找错误?
当前回答
要检查空值,请使用以下命令:
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有点困难;
其他回答
实际上,我认为你可能需要使用if(value!==null&&value!==undefined)因为如果使用if(value),也可能会过滤0或false值。
考虑这两个功能:
const firstTest = value => {
if (value) {
console.log('passed');
} else {
console.log('failed');
}
}
const secondTest = value => {
if (value !== null && value !== undefined) {
console.log('passed');
} else {
console.log('failed');
}
}
firstTest(0); // result: failed
secondTest(0); // result: passed
firstTest(false); // result: failed
secondTest(false); // result: passed
firstTest(''); // result: failed
secondTest(''); // result: passed
firstTest(null); // result: failed
secondTest(null); // result: failed
firstTest(undefined); // result: failed
secondTest(undefined); // result: failed
在我的情况下,我只需要检查值是否为null和未定义,我不想过滤0、false或“”值。所以我使用了第二个测试,但你可能也需要过滤它们,这可能会导致你使用第一个测试。
严格相等运算符:-
我们可以通过===
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的黑客代码))。
您可以使用lodash模块检查值是否为空或未定义
_.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
首先,您有一个没有函数体的return语句。这很可能会引发错误。
一个更干净的检查方法是简单地使用!操作员:
if (!pass || !cpass || !email || !cemail || !user) {
alert("fill all columns");
}