如何在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程序中查找错误?
当前回答
“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
其他回答
我找到了另一种方法来测试该值是否为空:
if(variable >= 0 && typeof variable === "object")
null同时充当数字和对象。比较null>=0或null<=0结果为true。比较null==0或null>0或null<0将导致false。但由于null也是一个对象,我们可以将其检测为null。
我做了一个更复杂的函数性质,它比typeof做得更好,并且可以被告知要包含或保持分组的类型
/*函数性质of(变量,[包含类型])包括的类型有null-null将导致“未定义”,如果包含,则将导致“null”NaN-NaN将导致“未定义”,或如果包含,将导致“NaN”-infinity-将负-无穷与“无限”分开number-将数字拆分为“int”或“double”array-将“array”与“object”分开空-空的“字符串”将导致“空”或空=未定义-空“字符串”将导致“未定义”*/函数性质of(v,…类型){/*null*/if(v===null)返回类型。includes('null')?“null”:“未定义”;/*NaN*/if(typeof v==“number”)返回(isNaN(v))?类型。包括('NaN')?“NaN”:“未定义”:/*-无穷大*/(v+1===v)?(types.includes('-infinity')&&v==Number.NEGATIVE_infinity)?“-无限”:“无限”:/*number*/(types.includes('number'))?(数字.isInteger(v))?“int”:“double”:“number”;/*array*/if(typeof v==“object”)return(types.includes('array')&&array.isArray(v))?“array”:“object”;/*空*/if(typeof v==“string”)返回(v==”)?types.includes(“空”)?“空”:/*空=未定义*/types.includes('empty=未定义')?“undefined”:“string”:“字符串”;否则返回v的类型}//演示let types=[null,“”,“string”,undefined,NaN,Infinity,-Invinity,false,“false”,true,“true”,0,1,-1,0.1,“test”,{var:1},[1,2],{0:1,1:2,长度:2}]for(i在类型中){console.log(“natureof”,类型[i],“=”,natureof(类型[i]”,“null”,“NaN”,“-ninfinity”,“number”,“array”,“empty=undefined”))}
乍一看,这似乎是一个简单的覆盖和严格之间的权衡。
==包含多个值,可以用更少的代码处理更多的场景。==是最严格的,这使得它可以预测。
可预测性总是获胜的,这似乎是一个万能的解决方案。
但这是错误的。尽管==是可预测的,但它并不总是产生可预测的代码,因为它忽略了场景。
const options = { };
if (options.callback !== null) {
options.callback(); // error --> callback is undefined.
}
通常,==为空检查执行更可预测的工作:
一般来说,null和undefined都意味着同一件事:“缺少了什么”。为了实现可预测性,您需要检查这两个值。然后==null做得很好,因为它正好覆盖了这两个值。(即==null等同于==null&&==未定义)在特殊情况下,您确实需要明确区分null和undefined。在这些情况下,最好使用严格的==未定义或==null。(例如,缺失/忽略/跳过和空/清除/删除之间的区别。)但这很少见。
这不仅是罕见的,也是需要避免的。不能在传统数据库中存储undefined。由于互操作性的原因,在API设计中也不应该依赖未定义的值。但即使你根本不做区分,你也不能假设未定义不会发生。我们周围的人都间接地采取了泛化空/未定义的行为(这就是为什么像这样的问题被封闭为“固执己见”的原因)。
所以,回到你的问题。使用==null没有错。它正是它应该做的。
// FIX 1 --> yes === is very explicit
const options = { };
if (options.callback !== null &&
options.callback !== undefined) {
options.callback();
}
// FIX 2 --> but == covers both
const options = { };
if (options.callback != null) {
options.callback();
}
// FIX 3 --> optional chaining also covers both.
const options = { };
options.callback?.();
只要在所有地方用==替换==即可。
==是一个松散或抽象的相等比较
==是严格的相等比较
有关更多详细信息,请参阅MDN关于平等比较和相同性的文章。
实际上,我认为你可能需要使用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或“”值。所以我使用了第二个测试,但你可能也需要过滤它们,这可能会导致你使用第一个测试。
“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