如何在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");
}
其他回答
检查错误条件:
// 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");
}
你可以使用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 {}
您也可以抛出自己的错误。看看这个。
“Object.is()”方法可用于确定两个值是否相同。因此,您可以使用它来检查对象是否为空。
检查空值
let testA = null; //null
console.log(Object.is(testA, null)); //true //null === null
if(Object.is(testA, null)) {
console.log("This is a Null Value");
}
Output:
true
This is a Null Value
检查未定义的值
let testB; //undefined
console.log(Object.is(testB, undefined)); //true //undefined === undefined
if(Object.is(testB, undefined)) {
console.log("This is an undefined Value");
}
Output:
true
This is an undefined Value
如果要同时检查undefined和null,请使用此选项。
let testC; //undefined
console.log(Object.is(testC, undefined)); //true //undefined === undefined
console.log(Object.is(testC, null)); //false //undefined === null
if (Object.is(testC, undefined) || Object.is(testC, null)){
console.log("The value is either undefined or null");
}
if (!(Object.is(testC, undefined) || Object.is(testC, null))){
console.log("The value is neither undefined nor null");
}
Output:
true
false
The value is either undefined or null
看看他们:https://onecompiler.com/javascript/3ymdqd34v
Mozilla解释:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
与操作员进行可选检查怎么样?
例如:
// check mother for null or undefined and
// then if mother exist check her children also
// this 100% sure it support and valid in JS today.
// Apart of that C# have almost the same operator using the same way
if (mother?.children) {
}
else {
// it is null, undefined, etc...
}
您可以使用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