Skip to content Skip to sidebar Skip to footer

Why New Boolean(false) Is True?

I do not understand what's going on var x=new Boolean(false) if(x){ console.log('plus') } console.log(x==false) //true Why if(x) returns true ?

Solution 1:

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 = newBoolean(false);
if (x) {
  // ...this code is executed
}

Directly from MDN.

Solution 2:

See MDN docs : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean .

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.

Solution 3:

Solution 4:

var x = new Boolean();

when use the keyword new ,the variable created is an object type, object type value is always true.

Solution 5:

Object is truthy. See http://james.padolsey.com/javascript/truthy-falsey/

so evaluating it without the implicit type conversion (when comparing to false) is true.

Post a Comment for "Why New Boolean(false) Is True?"