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

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

问题描述

  1. 如何检查变量是否为 nullundefined 以及 null 之间的区别是什么未定义?

  2. ===== 有什么区别(很难在 Google 上搜索===")?

解决方案

如何检查变量是否为 nullundefined...

变量是否为null:

if (a === null)//或者if (a == null)//但请参阅下面的注释

...但请注意,如果 aundefined,后者也将成立.

是否未定义:

if (typeof a === "undefined")//或者如果(a === 未定义)//或者if (a == undefined)//但请看下面的注释

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

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

if (!a) {//`a` 为假,包括 `undefined` 和 `null`//(还有`""`、`0`、`NaN`,以及[当然] `false`)}

这是由规范中的 ToBoolean 定义的.><块引用>

...nullundefined 有什么区别?

它们都是通常用来表示缺少某些东西的值.undefined 是更通用的,用作变量的默认值,直到为它们分配其他值,作为调用函数时未提供的函数参数的值,以及作为当你向一个对象要求它没有的属性时你得到的值.但它也可以明确用于所有这些情况.(不具有属性的对象与具有值为 undefined 的属性之间存在差异;调用具有值 undefined 的函数作为参数之间存在差异,并完全放弃这个论点.)

nullundefined 稍微具体一点:它是一个空白对象引用.当然,JavaScript 是松散类型的,但并非所有与 JavaScript 交互的东西都是松散类型的.如果浏览器中的 DOM 等 API 需要一个空白的对象引用,我们使用 null,而不是 undefined.同样,DOM 的 getElementById 操作返回一个对象引用 —要么是有效的(如果它找到了 DOM 元素),要么 null(如果没有).

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

<块引用>

=="和==="有什么区别

它们之间的唯一区别是 == 会进行类型强制以尝试使值匹配,而 ==== 不会.所以例如 "1" == 1 是真的,因为 "1" 强制为 1.但是 "1" === 1false,因为类型不匹配.("1" !== 1 为真.)=== 的第一个(真正的)步骤是操作数的类型是否相同?"如果答案为否",则结果为 false.如果类型相同,则它的作用与 == 的作用完全相同.

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

规范中的更多内容:

  1. How do I check a variable if it's null or undefined and what is the difference between the null and undefined?

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

解决方案

How do I check a variable if it's null or undefined...

Is the variable null:

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

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

Is it undefined:

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

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

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`)
}

This is defined by ToBoolean in the spec.

...and what is the difference between the null and 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 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).

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 "==="

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.

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

More in the spec:

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

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