Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does !new Boolean(false) equals false in JavaScript?

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?

like image 202
Michiel van Oosterhout Avatar asked Jan 01 '12 20:01

Michiel van Oosterhout


People also ask

What does New Boolean false ); return?

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"

Why is Boolean false true?

It is stated : 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.

Is false false in JavaScript?

A falsy (sometimes written falsey) value is a value that is considered false when encountered in a Boolean context. JavaScript uses type conversion to coerce any value to a Boolean in contexts that require it, such as conditionals and loops. The keyword false .

Why True == true is false in JS?

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.


2 Answers

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" 
like image 182
Adam Rackis Avatar answered Oct 13 '22 09:10

Adam Rackis


Because new Boolean returns an object as stated here.

The ! is defined as follows:

11.4.9 Logical NOT Operator ( ! )

The production UnaryExpression : ! UnaryExpression is evaluated as follows:

  1. Let expr be the result of evaluating UnaryExpression.

  2. Let oldValue be ToBoolean(GetValue(expr)).

  3. If oldValue is true, return false.

  4. Return true.

and:

9.2 ToBoolean

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

Table 11 — ToBoolean Conversions

Argument Type - Result

...

Object - true

So, it is an object, thus ToBoolean returns true, hence ! returns false.

like image 21
pimvdb Avatar answered Oct 13 '22 08:10

pimvdb