0

Why Object not true if checking consumer == true?

let consumer = {
  "checked": true,
  "ID": "680e543457-999fc-11e6",
  "fio": "John Doe",
  "office": {
    "ID": "fgh4d"
  },
  "phone": ""
};

console.log('consumer  == true', consumer == true); // false
console.log('consumer  === true', consumer === true); // false
console.log('Boolean(consumer)', Boolean(consumer));  // true
console.log('!consumer', !consumer); // false
if (consumer) console.log('1') // 1
Evgeniia I.
  • 44
  • 1
  • 5
  • 5
    Objects are *truthy* but aren't equal to the boolean value `true` – VLAZ Mar 02 '20 at 12:26
  • Learn about [Type coercion](https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion) – Mat J Mar 02 '20 at 12:29
  • Also relevant: https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – VLAZ Mar 02 '20 at 12:41

6 Answers6

0

Objects can't be equal to boolean.

but you can check the property checked

console.log('consumer.checked == true', consumer.checked == true); // true
console.log('consumer.checked === true', consumer.checked === true); // true
console.log('!consumer.checked', !consumer.checked); // false
if (consumer.checked) console.log('1') // 1
Refat Alsakka
  • 561
  • 2
  • 11
  • "*Objects can't be equal to boolean.*" they could. *this object* cannot but in general, it's valid `obj = { valueOf() { return true; } }; console.log(obj == true); //true` – VLAZ Mar 02 '20 at 13:31
0

In JS the == and === do not mean the same. When you use == you check if the element is equal to, however the === checks if the element is equal to and the same type as the element you are comparing it with exsample:

    1  === 1 // true
   "1" === 1 // false 
   "1" == 1  // true
    1  == 1  // true

However what you are doning when typing !consumer is that you are checking if the element exists (and is not undefined, NaN or Null)

Steinar
  • 149
  • 1
  • 16
  • `==` does not equal "element exists". `==` is not type safe and therefor compares only the content of the variable. `===` is type safe and compares the type and the content of the variable. – daPhantom Mar 02 '20 at 12:36
  • "*When you use `==` in this context you check if the consumer element exists*" 1. that is not true 2. Even assuming it was, then how does that explain why the result of `consumer == true` is `false`? – VLAZ Mar 02 '20 at 12:40
0

Additional to @VLAZ answer you can check the consumer object for undefined which results in a boolean expression.

let consumer = {
  "checked": true,
  "ID": "680e543457-999fc-11e6",
  "fio": "John Doe",
  "office": {
     "ID": "fgh4d"
  },
  "phone": ""
};

console.log('consumer  != undefined', consumer != undefined); // true
console.log('consumer  !== undefined', consumer !== undefined); // true

*edit: format

daPhantom
  • 119
  • 2
  • 9
0
  1. consumer == true

    here consumer is object, and true is boolean. Since == operator converts both variable values into same type, even though values are different. So it is False.

    for example if you compare 1 == '1' it will be true. but comparing object with boolean gonna be false only.

  2. consumer === true.

    === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

    for example, if you compare 1 === '1' it will be false. it will be true if you compare 1 === 1. But in your case, object definitely false if you compare with boolean.

  3. !consumer

    Basically Not operator(!) reverses the boolean result of the operand. consumer holds an object, so boolean result of the operand will be true. Not operator reverses it. so it is false.

  4. Consumer holds an object. so It will be True. so that If condition says True. Even consumer holds empty object({}) it will be True only. because it has own property.

    var a = {}; a.hasOwnProperty

a8hok
  • 153
  • 1
  • 2
  • 10
0

Thanks to everyone, i found what I needed, may be it would be helpful for enyone. As MDN web docs says

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

And also:

The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object, an empty array ([]), or the string "false", create an object with an initial value of true.

So

Boolean(consumer) // true
Evgeniia I.
  • 44
  • 1
  • 5
0

Your consumer isn't a boolean, so it's not equal to true. Looks like you want to see if the value is truthy of falsy.

!consumer checks if the value is falsy.

You can do this process again:

!!consumer checks if the value is truthy.

More info about understanding truthy and falsy values in javascript

  • "*Your consumer is equal to [object literal]*" it's not really correct though, though. `obj = { foo: "bar"}; obj == { foo: "bar" }` will return `false` since it is *not* equal to the contents of the `{ foo: "bar"}` object. Objects are compared by reference, not by shape or content. – VLAZ Mar 02 '20 at 13:28
  • Interesting! I didn't know, updated the answer, thanks! – Danielle Klaasen Mar 04 '20 at 08:53