2

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 ?

Govind Singh
  • 14,083
  • 12
  • 58
  • 94
Michael Phelps
  • 2,911
  • 5
  • 26
  • 56

7 Answers7

6

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.

Cerbrus
  • 60,471
  • 15
  • 115
  • 132
CodingIntrigue
  • 65,670
  • 26
  • 159
  • 166
  • only when passed to a conditional statement ? – Michael Phelps Jun 17 '14 at 07:31
  • Well yes, but what else were you planning on using it for? – CodingIntrigue Jun 17 '14 at 07:32
  • Good job literally copying the MDN and posting it as an answer. You may want to closevote the question as looking for a offsite resource, instead. The "Directly from MDN" line was edited in later... – Cerbrus Jun 17 '14 at 07:32
  • 1
    @Cerbrus Thank you. Did I not disclose that clearly enough for you? And as for you edit, absolutely not. That's not what the OP asked for, but I provided it as it answers the question. That would imply that other answers which *don't* reference the MDN docs would be deemed invalid - which is incorrect. Anyway, not the place for either of our comments. Go to meta if you feel the need to. – CodingIntrigue Jun 17 '14 at 07:33
  • 1
    RGraham: looking over the "situation" again, (And admittedly, after a quick meta question), I understand why answers like this aren't necessarily a "bad" thing. – Cerbrus Jun 17 '14 at 08:01
5

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.

Samuel Caillerie
  • 8,032
  • 1
  • 24
  • 31
4

It's as simple as this:

x is a Boolean object. (Logging it will show: Boolean {})

All objects evaluate to true (See the first paragraph under "Description")

Cerbrus
  • 60,471
  • 15
  • 115
  • 132
  • 1
    `All objects evaluate to true` - While I know that it might be good to include a link to [**some documentation**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) so users who don't know that have a reference to something they can continue learning from in the future. Difference between feeding someone and teaching them how to fish -- or what ever the saying is :). Just my personal preference – Nope Jun 17 '14 at 07:39
  • 1
    @FrançoisWahl: Will do, thanks. – Cerbrus Jun 17 '14 at 07:41
2

var x = new Boolean();

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

Matt
  • 70,063
  • 26
  • 142
  • 172
sharonli
  • 21
  • 1
0

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.

Jonas Köritz
  • 2,494
  • 18
  • 32
0

You are using the Boolean function incorrectly: http://www.w3schools.com/js/js_booleans.asp

What you're looking for is:

var x = false;
if (Boolean(x)){

}

However, since you already know x will be true or false, and you're not evaluating an expression to see if it's true or false (as per the link), you don't need the function Boolean()

user3036342
  • 995
  • 6
  • 15
0

A Boolean class is not a primitive value. when you check

if (someObject) 

it will return true because

Boolean(someObject)

will always return true.

You have a couple of options here: 1. Use

if (valueOf(x))

to get the primitive value again

2. Remove the 'new' keyword

x = Boolean(false)

So you get the value of the Boolean function


Take a look at What is the purpose of new Boolean() in Javascript?

Community
  • 1
  • 1
Ran
  • 145
  • 1
  • 10