Why is that:
var bla = {
"1":"1",
"2":"2"
}
console.log(bla === true)
is false ?
Why is that:
var bla = {
"1":"1",
"2":"2"
}
console.log(bla === true)
is false ?
That's because bla
is an object not a boolean
.
console.log(typeof bla === 'object') //logs true
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
Your bla is an object and false is boolean.
The identity operator returns true if the operands are strictly equal (see above) with no type conversion.
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
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); }