如何在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”))}

试试看:

if (!variable && typeof variable === "object") {
    // variable is null
}

与操作员进行可选检查怎么样?

例如:

// 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...

}

要检查空值,请使用以下命令:

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有点困难;

空值的简单解决方案:

function isEmpty(value) {
        return (
            value === null || value === undefined || value === '' ||
            (Array.isArray(value) && value.length === 0) ||
            (!(value instanceof Date) && typeof value === 'object' && Object.keys(value).length === 0)
        );
    }