Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why new Boolean(false) is true? [duplicate]

Tags:

javascript

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 ?

like image 371
Michael Phelps Avatar asked Oct 21 '25 16:10

Michael Phelps


2 Answers

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.

like image 153
CodingIntrigue Avatar answered Oct 23 '25 06:10

CodingIntrigue


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.

like image 25
Samuel Caillerie Avatar answered Oct 23 '25 06:10

Samuel Caillerie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!