6

I don't quite understand why the if statement evaluates true but the console.log comparison does not.

I double checked the MDN documentation and they say {} is a truthy value. So why does my console.log statement disagree?

I did try, as a last ditch, using == instead of ===.

var test = {};
    
console.log(test);          
console.log(test === true); 
console.log({} === true);   
    
if ({}) {
    console.log('What the ?');
} 
Boann
  • 48,794
  • 16
  • 117
  • 146
yohohosilver
  • 367
  • 1
  • 2
  • 15
  • Simply because `x == true` is not a way of testing whether or not `x` is truthy. `Boolean(x) == true` is, `!!{} == true`. – Paul Jun 03 '18 at 22:59
  • 2
    *"Truthy"* does not mean *"is equal to true"*, it means *"is equal to true **when evaluated as a boolean***. Try converting it to a bool first: `console.log(!!{} === true);` – Tyler Roper Jun 03 '18 at 23:00
  • https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons - `===` checks 'is this the same as', whereas `==` checks 'can this be coerced to be the same as. An if statement can be considereed to be using the latter. so `{}` is not the same as `true`, but it is 'truth-y', that is, it can be coerced to true – Chris O'Kelly Jun 03 '18 at 23:01

3 Answers3

8

=== is not the way to check whether a value is truthy in the way you are checking. For example, all the following are truthy. But if you try to do === with true they will result in false other than the truthy value true

if (true)
if ({})
if ([])
if (42)
if ("foo")
if (new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)

console.log(true === true);
console.log({} === true);
console.log([] === true);
console.log(42 === true);
console.log("foo" === true);
console.log((new Date()) === true);
console.log(-42 === true);
console.log(3.14 === true);
console.log(-3.14 === true);
console.log(Infinity === true);
console.log(-Infinity === true);

You can check truthy by using !!value. For example

var test = {};
console.log(!!test === true);

Similarly we can check for all the truthy above as following:

console.log(!!true === true);
console.log(!!{} === true);
console.log(!![] === true);
console.log(!!42 === true);
console.log(!!"foo" === true);
console.log(!!(new Date()) === true);
console.log(!!-42 === true);
console.log(!!3.14 === true);
console.log(!!-3.14 === true);
console.log(!!Infinity === true);
console.log(!!-Infinity === true);
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
  • Thoughts on use of !! as far as readability/properness of it? I'm in school right now for JS, so still learning. Thank you – yohohosilver Jun 03 '18 at 23:05
  • 2
    If you want it more readable, you can always use `Boolean(val)` instead of `!!val` to convert to boolean – Luca Kiebel Jun 03 '18 at 23:07
  • 4
    @MattWeber `!!x` is commonly used to convert a value to a boolean. It is readable because it is a widely recognized. If you find `Boolean(x)` more readable, use that; both are equally correct. – Paul Jun 03 '18 at 23:08
0

{} is truthy in JavaScript, that's why the if block is executed.

However, it is not strictly true so the triple equal comparison gives you false.

More info:

pyb
  • 4,813
  • 2
  • 27
  • 45
0

=== is strict equality. It first checks whether the types of its operands are the same, then whether they have the same value.

In {} === true, the left side has type object while the right side has type boolean, so it evaluates to false.

Many values are not equal to true but are still truthy. Similarly, there are values that are falsy but not equal to false.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 2
    Both these answers focus on the `===` even though the OP mentions that he observed the same thing with `==`. – Paul Jun 03 '18 at 23:02
  • 1
    @Paulpro That's because OP's question focuses on `===`. My last paragraph is a general statement about equality that applies to both `===` and `==`. – melpomene Jun 03 '18 at 23:03
  • OP's question doesn't focus on `===`. He wants to know why `{} == true` is `false`, even though `{}` is truthy. – Paul Jun 03 '18 at 23:05
  • 1
    @Paulpro That's not what the question says: "*So why does my console.log statement disagree?*" And the `console.log` statements all use `===`. – melpomene Jun 03 '18 at 23:06