1

Why is that:

var bla = {
    "1":"1",
    "2":"2"    
}

console.log(bla === true) 

is false ?

http://jsfiddle.net/nottinhill/jdxvnea1/

Nabil Kadimi
  • 9,428
  • 2
  • 49
  • 56
stevek-pro
  • 13,672
  • 14
  • 81
  • 134

4 Answers4

4

That's because bla is an object not a boolean.

console.log(typeof bla === 'object') //logs true

check this Q/A for more help on that

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 78,842
  • 31
  • 152
  • 211
3

By using === you are comparing without doing a type conversion, so variable with different types will never be equal.

> bla = {"1": "1", "2": "2"}

> bla == true
=> false

> bla === true
=> false

> bla === false
=> false

> Boolean(bla) === true        // Do it like this
=> true
Nabil Kadimi
  • 9,428
  • 2
  • 49
  • 56
  • 1
    bla == true => true is not correct. please show me jsfiddle where it works like you say. – stevek-pro Nov 18 '14 at 12:51
  • @SirBenBenji Yes, it's not, thus the comment `// Do it like this`, I don't know how it got there, I left my computer for a few moments HA HA... – Nabil Kadimi Nov 18 '14 at 12:56
1

Your bla is an object and false is boolean.

  • Strict equality ===

The identity operator returns true if the operands are strictly equal (see above) with no type conversion.

  • Equality ==

The equality operator converts the operands if they are not of the same type, 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 string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

Sources:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

Beri
  • 10,745
  • 4
  • 30
  • 53
0

you cannot equate a object to a boolean. you can do with the object index or check for the existence of the object like this

console.log(bla[1] == true)
or 
if(bla)  { console.log(true); }