admin管理员组文章数量:1417070
what does the term [0] == ![0]
means? Though they return true
.But i need to explain how it returns true
as type of [0] is object and ![0] returns boolean? So how they are equal? Thanks
what does the term [0] == ![0]
means? Though they return true
.But i need to explain how it returns true
as type of [0] is object and ![0] returns boolean? So how they are equal? Thanks
- Did you mean JavaScript? If not, how is this related to jQuery? – Louis Ricci Commented Jan 29, 2013 at 20:34
-
2
==
doesn't pare types – wirey00 Commented Jan 29, 2013 at 20:34 - 1 Why so many close-as-dupe votes? This is not a dupe of that. – bfavaretto Commented Jan 29, 2013 at 20:46
-
Why are people even talking about
===
? This has nothing to do with the difference between==
and===
– Ian Commented Jan 29, 2013 at 20:46
3 Answers
Reset to default 8![0]
is simply false
, since all non-null
objects cast to true
.
When paring [0]
and false
, they are converted to numbers - don't ask why, that's just the way it is. [0]
is first converted to the string "0"
(arrays cast to strings by concatenating the entries with ,
for a separator), which is then the number 0
. false
is cast to the number 0
, and there you have it: [0] == ![0]
is equivalent to 0 == 0
, which is true.
To understand this, go through ![0]
expression first. It evaluates to false
- as [0]
(as any Object in JS) is a truthy value. So the statement bees...
[0] == false
Now it's easier: false
is converted to 0
(for Boolean -> Number rule), and [0]
is converted by Object-To-Primitive rule - first to '0'
(String), then to 0
(Number). Obviously, 0
is equal to 0
. )
P.S. And yes, it may seem quite weird, but both...
[0] == false
... and ...
![0] == false
... evaluate to true
: the former is already explained, the latter is just false == false
. Anyone still surprised by those ==
Lint warnings? )
You have split the expression into multiple parts:
typeof([0]) // "object"
[0] == true // false
![0] == true // false
![0] == false // true
The reason for this because in JavaScript only the value 1
is implicitly converted to true, so all other values are converted to false. The ![0]
only negates a false expression thus it bees (false == false) == true
.
本文标签: javascriptNeed to explain the term 00Stack Overflow
版权声明:本文标题:javascript - Need to explain the term [0] == ![0] - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745259977a2650312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论