From the jQuery documentation on JavaScript types comes this snippet of code describing the behavior of strings when converted to booleans (that topic is not related to this question, but it’s just where I found the code):
!"" // true
!"hello" // false
!"true" // false
!new Boolean(false) // false
I get the first three examples, but I don’t get the last example, because:
new Boolean(false) == false //true
!false // true
So I would assume:
!new Boolean(false) // true
But instead:
!new Boolean(false) // false, mind = blown
What is this I don’t even…
Is it because:
new Boolean(false) === false // false
If so, what purpose does this serve?
new Boolean(false)returns an object. All objects (exceptdocument.allin browsers) are truthy.As a result,
!of any object will always befalse.To prove it to yourself, you can run this in your JavaScript console:
Also, you can use the strict equality operator
===to confirm thatnew Boolean(false)isn’t reallyfalse:Incidentally, calling the
Booleanfunction as a function—without thenew—actually does return a primitive: