1

Assume we have a condition, where a String "true" is compared to Boolean true. It returns false, though true == '1' returns true. If you do true == !!"true", this will return true, so it should mean, that since the beginning "true" was true. Is there any specific logic, that I am missing, or it just works with string representation of 0 and 1?

console.log(true == 'true')
Spectric
  • 30,714
  • 6
  • 20
  • 43
BaseScript
  • 381
  • 2
  • 7
  • "*so it should mean, that since the beginning "true"*" no, that doesn't actually logically follow. `!!42` is also `true` but `true == 42` is still `false` because they aren't loosely equal. `==` doesn't compare for what *converts* to `true`, after all, it compares both operands by converting them to a common base. – VLAZ Sep 15 '21 at 20:06
  • Added my two cents here: https://stackoverflow.com/a/69199489/6840789 – eden Sep 15 '21 at 20:36

1 Answers1

1

Because "true" is converted to NaN, while true is converted to 1. So they differ.

-- Source

Spectric
  • 30,714
  • 6
  • 20
  • 43
  • Thank you so much! It was really tricky:) – BaseScript Sep 15 '21 at 20:07
  • 1
    I'll add that the reason `!!"true"` returns true is that the double bang operator converts an `Object` into a `boolean`, returning `true` as long as the operand is not false, e.g. `0`, `null`, `undefined`, etc. Notably, `!!"0"` and `!!"1"` return `true` whereas `!!0` returns `false`. This is because the operands in the `true` examples are non-empty strings. – George Sun Sep 15 '21 at 20:07