1

Take the below code

console.log(navigator.geolocation == true);

enter image description here

As this is an object I believe it should be truthy, yet it always returns false.

Why is this?

seventeen
  • 557
  • 1
  • 6
  • 21
  • 2
    because `navigator.geolocation` is not loosely equal to `true`? – VLAZ May 11 '21 at 09:52
  • Are all object not truthy? When I console.log I see an Object. – seventeen May 11 '21 at 09:54
  • 1
    That's interesting; both `navigator.geolocation == true` and `navigator.geolocation == false` return `false`, when testing on my browser's console. – Haroldo_OK May 11 '21 at 09:56
  • 5
    @seventeen An object is truthy yes, but being truthy doesn't mean that it is loosely equal to `true`. – Ivar May 11 '21 at 09:56
  • 4
    truthy basically means *it converts to `true`* it does not mean *it is equal to `true`*. Do the conversion with `Boolean(navigator.geolocation)` or `!!navigator.geolocation` and you'd see it. – VLAZ May 11 '21 at 09:56
  • 2
    @Haroldo_OK because an object is not equal to `false`, either? It's been that way for...umm, literal decades now. – VLAZ May 11 '21 at 09:57
  • 2
    You can read it in the specs when abstract equality is true or false https://262.ecma-international.org/5.1/#sec-11.9.3 This list is pretty much self explaining – David P. May 11 '21 at 09:59
  • 1
    So, summarizing, the object is still "truey", but, if instead of using it directly on the `if` clause, you do a comparison against `true` itself, the promotion process that precedes the actual comparison makes it so they aren't the same, resulting in a counter-intuitive, if logical, result. – Haroldo_OK May 18 '21 at 11:32

0 Answers0