我如何检查一个变量,如果它是空或未定义,空和未定义之间的区别是什么? ==和===之间的区别是什么(很难搜索谷歌“===”)?


当前回答

我如何检查一个变量,如果它是空的或未定义…

变量是null吗?

if (a === null)
// or
if (a == null) // but see note below

...但注意,如果a未定义,后者也成立。

是否未定义:

if (typeof a === "undefined")
// or
if (a === undefined)
// or
if (a == undefined) // but see note below

...但是,请再次注意,最后一个是模糊的;如果a为空,它也为真。

现在,尽管有上述情况,检查它们的通常方法是利用它们是假的这一事实:

if (!a) {
    // `a` is falsey, which includes `undefined` and `null`
    // (and `""`, and `0`, and `NaN`, and [of course] `false`)
}

这是由规范中的ToBoolean定义的。

...null和undefined的区别是什么?

They're both values usually used to indicate the absence of something. undefined is the more generic one, used as the default value of variables until they're assigned some other value, as the value of function arguments that weren't provided when the function was called, and as the value you get when you ask an object for a property it doesn't have. But it can also be explicitly used in all of those situations. (There's a difference between an object not having a property, and having the property with the value undefined; there's a difference between calling a function with the value undefined for an argument, and leaving that argument off entirely.)

null比undefined更具体:它是一个空白对象引用。JavaScript当然是松散类型的,但并不是所有与JavaScript交互的东西都是松散类型的。如果像浏览器中的DOM这样的API需要一个空白的对象引用,我们使用null,而不是undefined。类似地,DOM的getElementById操作返回一个对象引用——一个有效的引用(如果它找到了DOM元素),或者null(如果没有)。

有趣的是(或者不有趣),他们有自己的类型。也就是说,null是null类型中的唯一值,而undefined是undefined类型中的唯一值。

“==”和“===”有什么区别?

它们之间的唯一区别是==将执行类型强制以尝试使值匹配,而===不会。例如"1" == 1为真,因为"1"强制为1。但是"1" === 1是假的,因为类型不匹配。("1" !== 1为真。)===的第一步(实际)是“操作数的类型相同吗?”,如果答案是“否”,则结果为假。如果类型相同,则执行==所执行的操作。

类型强制使用相当复杂的规则,并可能产生令人惊讶的结果(例如,"" == 0为真)。

详细说明如下:

等号比较(==,也叫“松散”等号) 严格相等比较(===)

其他回答

我如何检查一个变量,如果它是空的或未定义…

变量是null吗?

if (a === null)
// or
if (a == null) // but see note below

...但注意,如果a未定义,后者也成立。

是否未定义:

if (typeof a === "undefined")
// or
if (a === undefined)
// or
if (a == undefined) // but see note below

...但是,请再次注意,最后一个是模糊的;如果a为空,它也为真。

现在,尽管有上述情况,检查它们的通常方法是利用它们是假的这一事实:

if (!a) {
    // `a` is falsey, which includes `undefined` and `null`
    // (and `""`, and `0`, and `NaN`, and [of course] `false`)
}

这是由规范中的ToBoolean定义的。

...null和undefined的区别是什么?

They're both values usually used to indicate the absence of something. undefined is the more generic one, used as the default value of variables until they're assigned some other value, as the value of function arguments that weren't provided when the function was called, and as the value you get when you ask an object for a property it doesn't have. But it can also be explicitly used in all of those situations. (There's a difference between an object not having a property, and having the property with the value undefined; there's a difference between calling a function with the value undefined for an argument, and leaving that argument off entirely.)

null比undefined更具体:它是一个空白对象引用。JavaScript当然是松散类型的,但并不是所有与JavaScript交互的东西都是松散类型的。如果像浏览器中的DOM这样的API需要一个空白的对象引用,我们使用null,而不是undefined。类似地,DOM的getElementById操作返回一个对象引用——一个有效的引用(如果它找到了DOM元素),或者null(如果没有)。

有趣的是(或者不有趣),他们有自己的类型。也就是说,null是null类型中的唯一值,而undefined是undefined类型中的唯一值。

“==”和“===”有什么区别?

它们之间的唯一区别是==将执行类型强制以尝试使值匹配,而===不会。例如"1" == 1为真,因为"1"强制为1。但是"1" === 1是假的,因为类型不匹配。("1" !== 1为真。)===的第一步(实际)是“操作数的类型相同吗?”,如果答案是“否”,则结果为假。如果类型相同,则执行==所执行的操作。

类型强制使用相当复杂的规则,并可能产生令人惊讶的结果(例如,"" == 0为真)。

详细说明如下:

等号比较(==,也叫“松散”等号) 严格相等比较(===)

如果你的(逻辑)检查是否定的(!),你想捕获JS的null和undefined(因为不同的浏览器会给你不同的结果),你会使用限制性较低的比较: 例如:

var ItemID = Item.get_id();
if (ItemID != null)
{
 //do stuff
}

这将捕获null和undefined

规范是这些问题的完整答案。以下是摘要:

For a variable x, you can: check whether it's null by direct comparison using ===. Example: x === null check whether it's undefined by either of two basic methods: direct comparison with undefined or typeof. For various reasons, I prefer typeof x === "undefined". check whether it's one of null and undefined by using == and relying on the slightly arcane type coercion rules that mean x == null does exactly what you want. The basic difference between == and === is that if the operands are of different types, === will always return false while == will convert one or both operands into the same type using rules that lead to some slightly unintuitive behaviour. If the operands are of the same type (e.g. both are strings, such as in the typeof comparison above), == and === will behave exactly the same.

更多阅读:

安格斯·克罗尔的《真理、平等和JavaScript》 Andrea Giammarchi的JavaScript强制解密 JavaScript常见问题:JavaScript类型转换

广告1。Null不是全局对象属性的标识符,不像undefined可以

让x;/ /定义 让y =零;/ /空 让z = 3;//有值 // 'w' //未声明 If (!x) console.log('x为空或未定义'); 如果(!y) console.log('y是空的或未定义的'); If (!z) console.log('z为空或未定义'); Try {if(w) 0} catch(e) {console.log('w is undefined ')} // typeof不抛出未声明变量的异常 If (typeof w === 'undefined') console.log('w is undefined');

广告2。===检查值和类型。==不需要相同的类型,并在比较之前进行隐式转换(使用. valueof()和. tostring())。这里你有所有(src):

if

==(否定!=)

===(它的否定!==)

尝试不同的逻辑。你可以使用下面的代码检查所有(4)条件验证,如不null,不空白,不定义,不为零,只使用这个代码(!(!(变量)))在javascript和jquery。

function myFunction() {
var data;  //The Values can be like as null, blank, undefined, zero you can test

if(!(!(data)))
{
   //If data has valid value
    alert("data "+data);
} 
else 
{
    //If data has null, blank, undefined, zero etc.
    alert("data is "+data);
}

}