Why does !new Boolean(false) equals false in JavaScript?
new Boolean(false)
returns an object. All objects (except document.all
in browsers) are truthy.
As a result, !
of any object will always be false
.
To prove it to yourself, you can run this in your JavaScript console:
(typeof new Boolean(false)) // "object"
Also, you can use the strict equality operator ===
to confirm that new Boolean(false)
isn’t really false
:
new Boolean(false) === false // false
Incidentally, calling the Boolean
function as a function—without the new
—actually does return a primitive:
!Boolean(false) // true
(typeof Boolean(false)) // "boolean"
Why new Boolean(false) is true?
Says so in the docs:
Any object whose value is not undefined or null, including a Boolean
object whose value is false, evaluates to true when passed to a
conditional statement. For example, the condition in the following if
statement evaluates to true
x = new Boolean(false);
if (x) {
// ...this code is executed
}
Directly from MDN.
Why `Number(new Boolean(false)) === 0`
As @ASDFGerte mentioned. This is because the ToNumber()
method which is called by the Number()
constructor will call .ToPrimitive()
on the argument if an object is passed. This is why it's treated as a Boolean primitive rather than an object.
why in JavaScript's: obj = new Boolean(false) , (obj && true) is true, and (obj || false) is false?
There are two concepts which need to be considered here:
obj = new Boolean(false)
creates an Object, whose values is false
. The Object itself is considered truthy, it's value (which you get with toString()
or valueOf()
) of course is the boolean value false
.
(x||y)
returns the first truthy value (or if none are present, the last falsy value) and
(x&&y)
returns the first falsy value (or if none are present, the last truthy value).
So (obj||false)
returns your Boolean Object, (obj&&true)
returns the second (true
) value.
The further preceeding depends on the context of your expression.
"obj && true: " + (obj && true)
demands a string context, so toString()
is called on your Boolean Object, returning it's value which is false
(While the object itself is truthy!).
Furthermore,
(obj && true) == true
compares true == true
which of course is true. However,
(obj || true) == true
does a lot of type coercion §11.9.3 and compares
ToPrimitive(obj) == ToNumber(true)
(§9.1 and §9.3) which results in NaN == 1
which yields false.
The results get more predictable if you use the strict equality operator §11.9.6.
new Boolean(name) always returns true
Boolean
doesn't do what you think it does. It's an object, and objects are always truthy:
> typeof new Boolean(false)
"object"
> if (new Boolean(false)) console.log("It's true!");
It's true!
To convert a value to a primitive boolean
type instead, a common way is to negate it twice:
const nameCheck = !!name;
You can also invoke Boolean
as a function, but I'd stay away from that because it's rather confusing that it doesn't return an actual Boolean
:
const nameCheck = Boolean(name);
alert(new Boolean(false)) vs console.log(new Boolean(false))
alert()
displays the .toString()
value of the argument that is passed to it.
The toString()
value of a Boolean
object is either true
or false
.
Related Topics
How to Add a Tooltip to an Svg Graphic
Getting the Return Value of JavaScript Code in Selenium
What Does [Object Object] Mean? (Javascript)
How to Use Ajax to Do File Upload
Set a Cookie to Httponly via JavaScript
Manipulating Innerhtml Removes the Event Handler of a Child Element
How to Destructure into Dynamically Named Variables in Es6
Force Download Through Js or Query
Es6 Array Destructuring Weirdness
Relation Between Commonjs, Amd and Requirejs
Send Message to Specific Client with Socket.Io and Node.Js
How to Scroll to Specific Item Using Jquery
Jquery.Inarray(), How to Use It Right
Using Jquery to Replace One Tag with Another
Idiomatically Find the Number of Occurrences a Given Value Has in an Array
(![]+[])[+[]]... Explain Why This Works