[英]In JavaScript, is using `a == null` in fact the same as `a === null || a === undefined`?
It took me a while to note to always use ===
in JavaScript instead of ==
.我花了一段时间才注意到在 JavaScript 中总是使用
===
而不是==
。
But today I noticed但是今天我注意到
if (a == null) { ... }
seems identical as似乎与
if (a === null || a === undefined) { ... }
from a table in the MDN docs . 来自 MDN 文档中的表格。
I hope to know:我希望知道:
a == null
instead of a === null || a === undefined
a == null
而不是a === null || a === undefined
a === null || a === undefined
? a === null || a === undefined
?Yes, they are identical, except for one case of document.all
(please see the first comment).是的,它们是相同的,除了
document.all
一个案例(请参阅第一条评论)。 We can see that in the ==
chart, null
and undefined
compares to true
only with itself and each other: null == null
is true
, and null == undefined
is true
, etc, and nothing else.我们可以看到,在
==
图表中, null
和undefined
仅在自身和彼此之间与true
进行比较: null == null
是true
,而null == undefined
是true
等等,没有别的。 So when we use something == null
, the only something
that can give a true
is null
and undefined
:所以,当我们使用
something == null
,唯一的something
,可以给一个true
的null
和undefined
:
Even the TC39 is using it that way:甚至 TC39 也是这样使用它的:
https://github.com/tc39/proposal-optional-chaining/blob/master/README.md https://github.com/tc39/proposal-optional-chaining/blob/master/README.md
a?.b // undefined if `a` is null/undefined, `ab` otherwise. a == null ? undefined : ab
However, I don't recommend using it in common daily coding.但是,我不建议在日常编码中使用它。 That's because, if the code
a == null
is read by other programmers, they may immediately change it to a === null
and if the test cases are not able to catch it, then a subtle bug might be introduced in the system.这是因为,如果代码
a == null
被其他程序员读取,他们可能会立即将其更改为a === null
,如果测试用例无法捕获它,则可能会在系统中引入一个微妙的错误。
In an open source project where you have the ultimate decision for what goes in and what doesn't, then it can be used, with your code review and approval.在一个开源项目中,您对什么可以加入什么不可以有最终决定,然后可以使用它,并经过您的代码审查和批准。 Or, if you use it once or twice and then with proper comment up above to state what you are trying to do.
或者,如果您使用它一次或两次,然后在上面加上适当的注释来说明您要做什么。
Otherwise, most other programmers may think if you really aim to compare just to null
and not considering for undefined
.否则,大多数其他程序员可能会认为您是否真的只想与
null
进行比较而不考虑undefined
。 If you write a === null || a === undefined
如果你写
a === null || a === undefined
a === null || a === undefined
, then most everybody knows exactly what you are trying to do. a === null || a === undefined
,那么大多数人都知道你想要做什么。
A third side effect is, if you say a == null
is identical to a === null || a === undefined
第三个副作用是,如果你说
a == null
等同于a === null || a === undefined
a === null || a === undefined
, you could attract tons of users on Stack Overflow, downvoting on you, saying don't ever use ==
in JavaScript. a === null || a === undefined
,你可以在 Stack Overflow 上吸引大量用户,对你投反对票,说永远不要在 JavaScript 中使用==
。
An interesting fact is, even Babel translate a?.b
to compare with both null
and undefined
( void 0
), instead of just using == null
.一个有趣的事实是,即使 Babel 也将
a?.b
翻译a?.b
与null
和undefined
( void 0
) 进行比较,而不是仅使用== null
。 Babel doesn't really have to, but maybe it does that so that when a programmer reads the compiled code, it is more clear. Babel 并不是真的必须这样做,但也许它这样做是为了让程序员在阅读编译后的代码时更清楚。
But I don't know how many interviewers have asked me if the line if (a == null)
is good, and when I said we should use ===
, then I was viewed as just one step above greenhorn, because all the "pros" know that a == null
is for checking whether a
is null
or undefined
.但是我不知道有多少面试官问过我这行
if (a == null)
是否好,当我说我们应该使用===
,我被认为只是比新手高出一步,因为所有的“专业人士”知道a == null
用于检查a
是null
还是undefined
。 So while maybe it is not good to actually use it in the code, it is good for testing if somebody is a pro.因此,虽然在代码中实际使用它可能并不好,但它有利于测试某人是否是专业人士。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.