JavaScript检查null与undefined以及==和===之间的差异 [英] JavaScript checking for null vs. undefined and difference between == and ===

查看:179
本文介绍了JavaScript检查null与undefined以及==和===之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,我知道必须有一些线索涵盖这个主题。但我使用搜索并没有得到符合我需要的答案。所以我们走了:

I know, I know there must be some threads covering this topic. But I used the search and didn't get the answer which fits my needs. So here we go:


  1. 如果变量是 null undefined null undefined

==和===之间有什么区别(很难在Google上搜索 === )?

What is the difference between "==" and "===" (it's hard to search Google for === )?


推荐答案


如果变量 null undefined ...

变量 null

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

...但请注意,如果 a 为<后者,后者也将为真code> undefined

...but note the latter will also be true if a is undefined.

是否未定义

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

...但请注意,最后一个是模糊的;如果 a null ,也是如此。

...but again, note that the last one is vague; it will also be true if a is null.

现在,尽管如此,检查这些的通常方式是使用他们 falsey 的事实:

Now, despite the above, the usual way to check for those is to use the fact that they're falsey:

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

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

This is defined by ToBoolean in the spec.


... null 和<$之间有什么区别? c $ c> undefined ?

它们都是通常用来表示没有东西的值。 undefined 是更通用的一个,用作变量的默认值,直到它们被赋予一些其他值,作为函数时未提供的函数参数的值被调用,并且当你向对象询问它没有的属性时获得的值。但它也可以在所有这些情况下明确使用。 (没有属性的对象与具有值 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 is slightly more specific than undefined: It's a blank object reference. JavaScript is loosely typed, of course, but not all of the things JavaScript interacts with are loosely typed. If an API like the DOM in browsers needs an object reference that's blank, we use null, not undefined. And similarly, the DOM's getElementById operation returns an object reference — either a valid one (if it found the DOM element), or null (if it didn't).

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

Interestingly (or not), they're their own types. Which is to say, null is the only value in the Null type, and undefined is the only value in the Undefined type.


==和===之间有什么区别

What is the difference between "==" and "==="

它们之间的唯一区别是 == 会输入强制来尝试获取匹配的值,并且 === 不会。所以例如1== 1 是真的,因为1强制到 1 。但是1=== 1 false ,因为类型不匹配。 (1!== 1 为真。) === 的第一步(实际)是操作数的类型是否相同?如果答案为否,则结果为 false 。如果类型相同,它确实与 == 相同。

The only difference between them is that == will do type coercion to try to get the values to match, and === won't. So for instance "1" == 1 is true, because "1" coerces to 1. But "1" === 1 is false, because the types don't match. ("1" !== 1 is true.) The first (real) step of === is "Are the types of the operands the same?" and if the answer is "no", the result is false. If the types are the same, it does exactly what == does.

类型强制使用非常复杂的规则和可以有惊人的结果(例如,== 0 为真)。

Type coercion uses quite complex rules and can have surprising results (for instance, "" == 0 is true).

规范中的更多信息:

  • Abstract Equality Comparison (==, also called "loose" equality)
  • Strict Equality Comparison (===)

这篇关于JavaScript检查null与undefined以及==和===之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆