JavaScript 检查 null 与 undefined 以及 == 和 === 之间的区别
如何检查变量是否为
null
或undefined
以及null
和undefined 之间的区别
code>?==
和===
之间有什么区别(很难在 Google 中搜索“===”)?
How do I check a variable if it's
null
orundefined
and what is the difference between thenull
andundefined
?What is the difference between
==
and===
(it's hard to search Google for "===" )?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
变量是否为
null
:...但请注意,后者也将是如果
a
为未定义
,则为 true。是
未定义
吗:……但再次请注意,最后一个是模糊的;如果
a
为null
,它也为真。现在,尽管有上述情况,检查这些的通常方法是利用它们是错误的事实:
这是由 ToBoolean 。
它们都是通常用来表示某物不存在的值。
undefined
是更通用的一种,用作变量的默认值,直到它们被分配其他值为止,作为调用函数时未提供的函数参数的值,以及作为当你向一个对象请求它没有的属性时你得到的值。但它也可以在所有这些情况下明确使用。 (不具有属性的对象与具有值为undefined
的属性之间存在差异;使用参数值undefined
调用函数之间存在差异,并完全保留该参数。)null
比undefined
稍微具体一些:它是一个空白对象引用。当然,JavaScript 是松散类型的,但并非所有与 JavaScript 交互的东西都是松散类型的。如果像浏览器中的 DOM 这样的 API 需要一个空白的对象引用,我们使用null
,而不是undefined
。同样,DOM 的 getElementById 操作返回一个对象引用 — 要么是一个有效的引用(如果它找到了 DOM 元素),要么是null
(如果没有)。有趣的是(或没有),他们有自己的类型。也就是说,
null
是 Null 类型中的唯一值,而undefined
是 Undefined 类型中的唯一值。它们之间的唯一区别是
==
会进行类型强制以尝试获取匹配的值,而== = 不会。例如,
"1" == 1
为 true,因为"1"
强制为1
。但是"1" === 1
是 false,因为类型不匹配。 ("1" !== 1
为 true。)===
的第一步(真正的)是“操作数的类型是否相同?”如果答案为“否”,则结果为false
。如果类型相同,则它的作用与==
完全相同。类型强制使用相当复杂的规则,并且可能会产生令人惊讶的结果(例如,
"" == 0
为 true)。规范中的更多内容:
===
)Is the variable
null
:...but note the latter will also be true if
a
isundefined
.Is it
undefined
:...but again, note that the last one is vague; it will also be true if
a
isnull
.Now, despite the above, the usual way to check for those is to use the fact that they're falsey:
This is defined by ToBoolean in the spec.
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 valueundefined
; there's a difference between calling a function with the valueundefined
for an argument, and leaving that argument off entirely.)null
is slightly more specific thanundefined
: 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 usenull
, notundefined
. And similarly, the DOM'sgetElementById
operation returns an object reference — either a valid one (if it found the DOM element), ornull
(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, andundefined
is the only value in the Undefined type.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 to1
. 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 isfalse
. 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:
==
, also called "loose" equality)===
)差异是微妙的。
在 JavaScript 中,
未定义
变量是从未声明或从未赋值的变量。例如,假设您声明var a;
,那么a
将是undefined
,因为它从未被分配任何值。但如果您随后分配
a = null;
,则a
现在将为null
。在 JavaScript 中,null
是一个对象(如果您不相信我,请在 JavaScript 控制台中尝试typeof null
),这意味着 null 是一个值(事实上,甚至>未定义
是一个值)。示例:
这在函数参数中非常有用。您可能想要一个默认值,但认为 null 是可以接受的。在这种情况下,您可以这样做:
如果省略
可选
参数doSomething(1, 2),那么
可选将是“三”
字符串,但是如果您传递doSomething(1, 2, null)
,则可选将为null
。至于相等
==
和严格相等===
比较器,第一个是弱类型,而严格相等也会检查值的类型。这意味着0 == "0"
将返回 true;而0 === "0"
将返回 false,因为数字不是字符串。您可以使用这些运算符来检查
undefined
和null
。例如:最后一种情况很有趣,因为它允许您检查变量是否未定义或为 null,仅此而已:
The difference is subtle.
In JavaScript an
undefined
variable is a variable that as never been declared, or never assigned a value. Let's say you declarevar a;
for instance, thena
will beundefined
, because it was never assigned any value.But if you then assign
a = null;
thena
will now benull
. In JavaScriptnull
is an object (trytypeof null
in a JavaScript console if you don't believe me), which means that null is a value (in fact evenundefined
is a value).Example:
This can prove useful in function arguments. You may want to have a default value, but consider null to be acceptable. In which case you may do:
If you omit the
optional
parameterdoSomething(1, 2) then
optional will be the"three"
string but if you passdoSomething(1, 2, null)
then optional will benull
.As for the equal
==
and strictly equal===
comparators, the first one is weakly type, while strictly equal also checks for the type of values. That means that0 == "0"
will return true; while0 === "0"
will return false, because a number is not a string.You may use those operators to check between
undefined
annull
. For example:The last case is interesting, because it allows you to check if a variable is either undefined or null and nothing else:
规范是获取这些问题的完整答案的地方。摘要如下:
x
,您可以:===
直接比较来检查它是否为null
。示例:x === null
未定义
:直接与undefined
或typeof
进行比较。由于各种原因,我更喜欢typeof x === "undefined"
。==
并依赖于表示的有点神秘的类型强制规则来检查它是否为
正是您想要的。null
和undefined
之一x == null==
和===
之间的基本区别是,如果操作数的类型不同,===
将始终返回false
而==
将使用 规则会导致一些稍微不直观的行为。如果操作数的类型相同(例如都是字符串,如上面的typeof
比较),则==
和===
行为将完全相同。更多阅读:
The spec is the place to go for full answers to these questions. Here's a summary:
x
, you can:null
by direct comparison using===
. Example:x === null
undefined
by either of two basic methods: direct comparison withundefined
ortypeof
. For various reasons, I prefertypeof x === "undefined"
.null
andundefined
by using==
and relying on the slightly arcane type coercion rules that meanx == null
does exactly what you want.==
and===
is that if the operands are of different types,===
will always returnfalse
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 thetypeof
comparison above),==
and===
will behave exactly the same.More reading:
只需检查变量是否具有有效值,如下所示:
它将返回 true
just check if a variable has a valid value like this :
it will return true if variable does't contain :
广告 1.
null
不是全局对象属性的标识符,例如undefined
可以广告 2.
===
检查值和类型。==
不需要相同的类型,并在比较之前进行隐式转换(使用.valueOf()
和.toString()
)。这里你有所有(src):if
== (其否定 !=)
=== (其否定 !==)
Ad 1.
null
is not an identifier for a property of the global object, likeundefined
can beAd 2. The
===
check values and types. The==
dont require same types and made implicit conversion before comparison (using.valueOf()
and.toString()
). Here you have all (src):if
== (its negation !=)
=== (its negation !==)
undefined
表示变量尚未初始化。
示例:
equals(==)
它仅检查值是否等于而不是数据类型。
示例:
因为它只检查值。
严格等于(===)
检查值和数据类型应相同。
示例:
因为它检查数据类型 x 是基本类型并且 y 是布尔对象。
undefined
It means the variable is not yet intialized .
Example :
equals(==)
It only check value is equals not datatype .
Example :
Because it checks only value .
Strict Equals(===)
Checks the value and datatype should be same .
Example :
Because it checks the datatype x is a primitive type and y is a boolean object .
如果您的(逻辑)检查是针对否定(!)并且您想要捕获 JS
null
和undefined
(因为不同的浏览器会给您不同的结果),您将使用限制性较小的比较:例如:
这将捕获
null
和undefined
If your (logical) check is for a negation (!) and you want to capture both JS
null
andundefined
(as different Browsers will give you different results) you would use the less restrictive comparison:e.g.:
This will capture both
null
andundefined
尝试不同的逻辑。您可以使用下面的代码检查所有四(4)条件进行验证,例如非空,非空白,非未定义和非零仅在javascript和jquery中使用此代码(!(!(变量)))。
}
Try With Different Logic. You can use bellow code for check all four(4) condition for validation like not null, not blank, not undefined and not zero only use this code (!(!(variable))) in javascript and jquery.
}
您可以简单地使用条件语句来检查变量是否为 null 或未定义。
未定义表示变量已声明但未赋值
Null 表明某个变量被故意设置为 null;
Undefine 大部分时候是在无意的情况下发生的,而 null 则是出于有意的目的而发生的。
“==”和“===”的区别在于,第一个不比较变量的类型,而第二个也比较变量的类型。
这是两者之间的基本区别。
You can simply use conditional statements to check if the variable is null or undefined.
Undefined shows that a variable is declared but it is not assigned a value
Null shows that a variable is deliberately set to null;
Undefined happens most of the time in unintentional cases while null happens for intentional purposes.
The difference between "==" and "===" is that the first one does not compare the type of variable while the second one compares the type of variable as well.
That is the basic difference between these two.