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

Share Improve this question edited Jan 29, 2013 at 20:51 Facundo Casco 10.6k8 gold badges44 silver badges65 bronze badges asked Jan 29, 2013 at 20:32 CodegiantCodegiant 2,1501 gold badge22 silver badges31 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

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