[英]JavaScript checking for null vs. undefined and difference between == and ===
如何检查变量是否为null
或undefined
以及null
和undefined
之间有什么区别?
==
和===
什么区别(很难在 Google 上搜索“===”)?
如何检查变量是否为
null
或undefined
...
变量是否为null
:
if (a === null)
// or
if (a == null) // but see note below
...但请注意,如果a
为undefined
,后者也将成立。
是否undefined
:
if (typeof a === "undefined")
// or
if (a === undefined)
// or
if (a == undefined) // but see note below
...但同样,请注意最后一个是模糊的; 如果a
为null
它也将是 true 。
现在,尽管有上述情况,检查这些的通常方法是使用它们是false的事实:
if (!a) {
// `a` is falsey, which includes `undefined` and `null`
// (and `""`, and `0`, and `NaN`, and [of course] `false`)
}
...
null
和undefined
之间有什么区别?
它们都是通常用于表示缺少某些东西的值。 undefined
是更通用的一个,用作变量的默认值,直到为它们分配其他值,作为调用函数时未提供的函数参数的值,以及作为您询问时获得的值它没有的属性的对象。 但它也可以明确用于所有这些情况。 (不具有属性的对象与具有值为undefined
的属性之间存在差异;调用具有undefined
值的函数作为参数与完全undefined
使用该参数之间存在差异。)
null
比undefined
更具体:它是一个空白对象引用。 当然,JavaScript 是松散类型的,但并非所有与 JavaScript 交互的东西都是松散类型的。 如果像浏览器中的 DOM 这样的 API 需要一个空白的对象引用,我们使用null
,而不是undefined
。 同样,DOM 的getElementById
操作返回一个对象引用——一个有效的(如果它找到了 DOM 元素),或者null
(如果它没有找到)。
有趣的是(或不是),他们是他们自己的类型。 也就是说, null
是Null类型中唯一的值, undefined
是Undefined类型中唯一的值。
“==”和“===”有什么区别
它们之间的唯一区别是==
会执行类型强制以尝试使值匹配,而===
不会。 因此,例如"1" == 1
是真的,因为"1"
强制为1
。 但是"1" === 1
是false ,因为类型不匹配。 ( "1" !== 1
为真。) ===
的第一个(真正的)步骤是“操作数的类型是否相同?” 如果答案为“否”,则结果为false
。 如果类型相同,则它的作用与==
作用完全相同。
类型强制使用相当复杂的规则并且可能产生令人惊讶的结果(例如, "" == 0
为真)。
规范中的更多内容:
区别很微妙。
在 JavaScript 中, undefined
变量是从未被声明或从未赋值的变量。 假设你声明了var a;
例如,那么a
将是undefined
,因为它从未被赋予任何值。
但是如果你再分配a = null;
那么a
现在将为null
。 在 JavaScript 中, null
是一个对象(如果您不相信我,请在 JavaScript 控制台中尝试typeof null
),这意味着 null 是一个值(实际上甚至undefined
也是一个值)。
例子:
var a;
typeof a; # => "undefined"
a = null;
typeof null; # => "object"
这可以证明在函数参数中很有用。 您可能希望有一个默认值,但认为 null 是可以接受的。 在这种情况下,您可以这样做:
function doSomething(first, second, optional) {
if (typeof optional === "undefined") {
optional = "three";
}
// do something
}
如果省略optional
参数doSomething(1, 2) then
optional 将是"three"
字符串,但如果您传递doSomething(1, 2, null)
则 optional 将为null
。
至于相等==
和严格相等===
比较器,第一个是弱类型,而严格相等也会检查值的类型。 这意味着0 == "0"
将返回 true; while 0 === "0"
将返回 false,因为数字不是字符串。
您可以使用这些运算符在undefined
和null
之间进行检查。 例如:
null === null # => true
undefined === undefined # => true
undefined === null # => false
undefined == null # => true
最后一种情况很有趣,因为它允许您检查变量是否为 undefined 或 null 而不是其他:
function test(val) {
return val == null;
}
test(null); # => true
test(undefined); # => true
规范是为这些问题提供完整答案的地方。 这是一个总结:
x
,您可以:===
直接比较来检查它是否为null
。 示例: x === null
undefined
:与undefined
或typeof
直接比较。 由于各种原因,我更喜欢typeof x === "undefined"
。==
并依靠稍微神秘的类型强制规则来检查它是否为null
和undefined
之一,这意味着x == null
完全符合您的要求。==
和===
之间的基本区别在于,如果操作数的类型不同,则===
将始终返回false
而==
将使用导致一些稍微不直观的行为的规则将一个或两个操作数转换为相同类型。 如果操作数的类型相同(例如,两者都是字符串,例如在上面的typeof
比较中), ==
和===
行为将完全相同。更多阅读:
如何检查变量是否为空或未定义
只需检查变量是否具有这样的有效值:
if(variable)
如果变量不包含,它将返回真:
不明确的
这意味着变量尚未初始化。
例子 :
var x;
if(x){ //you can check like this
//code.
}
等于(==)
它只检查 value 是否等于而不是 datatype 。
例子 :
var x = true;
var y = new Boolean(true);
x == y ; //returns true
因为它只检查 value 。
严格等于(===)
检查值和数据类型应该相同。
例子 :
var x = true;
var y = new Boolean(true);
x===y; //returns false.
因为它检查数据类型 x 是原始类型而 y 是布尔对象。
广告 1. null
不是全局对象属性的标识符,就像undefined
可以是
let x; // undefined let y=null; // null let z=3; // has value // 'w' // is undeclared if(!x) console.log('x is null or undefined'); if(!y) console.log('y is null or undefined'); if(!z) console.log('z is null or undefined'); try { if(w) 0 } catch(e) { console.log('w is undeclared') } // typeof not throw exception for undelared variabels if(typeof w === 'undefined') console.log('w is undefined');
广告 2. ===
检查值和类型。 ==
不需要相同的类型并在比较之前进行隐式转换(使用.valueOf()
和.toString()
)。 在这里你有所有( src ):
如果
== (它的否定!= )
=== (它的否定!== )
如果您的(逻辑)检查是针对否定(!)并且您想同时捕获 JS null
和undefined
(因为不同的浏览器会给您不同的结果),您将使用限制较少的比较:例如:
var ItemID = Item.get_id();
if (ItemID != null)
{
//do stuff
}
这将捕获null
和undefined
尝试不同的逻辑。 您可以使用波纹管代码检查所有四(4)个条件以进行验证,例如非空、非空白、非未定义和非零,仅在 javascript 和 jquery 中使用此代码 (!(!(variable)))。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.