0

how to do you prove that boolean datatypes like "true" and "false "are converted to numbers when using the == operator in javaScript who knows whether the "numbers are" converted to booleans in javaScript when using the == operator see the below example:

console.log(true == 1);
console.log(false == 1);

How do you very surely say that in the above code the "boolean datatype" "true" and "false" is always converted to numbers when comparing them with "Numbers" using the "Loose Equality operator in javaScript (==)" who knows whether the "Numbers" are converted to the boolean datatype "true" and "false".

Please help me figure this out

palaѕн
  • 72,112
  • 17
  • 116
  • 136
Kevin
  • 209
  • 2
  • 11
  • 1
    Because the standard says so. – Teemu Mar 10 '20 at 06:46
  • is it always accurate – Kevin Mar 10 '20 at 06:46
  • 1
    Look up [Abstract Equality Comparison](http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-equality-comparison). (or, even better, never use `==`, use `===` instead) – CertainPerformance Mar 10 '20 at 06:47
  • 1
    Engines *almost always* conform to the specification. For something as fundamental as this, you can rely on any widely used engine conforming. If an engine *doesn't* conform to the specification, that's usually cause for a bug report and a fix by the engine-makers. Deliberate non-compliance in core JS is nearly unheard of. – CertainPerformance Mar 10 '20 at 06:48
  • Short of reading the JS engine's source code, you can't prove this is what happens. It is also not the important thing. The important thing (which you _can_ — and _did_ — prove) is that the results are _consistent_ with what the specification says. – Amadan Mar 10 '20 at 06:53
  • well now i wanna know between a "boolean" datatype and "Number" why people always say that booleans are coerced to numbers who knows whether the opposite happens i mean "who knows whether numbers are coerced to booleans when compared using the (Loose Equality operator)" – Kevin Mar 10 '20 at 06:56
  • if the boolean was not converted to number, `'true' == true` will return `true`, just as `"1" == 1` return `true`. but because different types converted to number, `1=="1"` because `1==1`, but `true != "true"` because `true` converted to `1` – Yosef Tukachinsky Mar 10 '20 at 06:58
  • Did you read the comments here at all? – Teemu Mar 10 '20 at 07:04
  • yes i did and i need more code examples to prove that booleans are converted to numbers in javaScript so that it will be easier for me to get it – Kevin Mar 10 '20 at 07:06
  • Any code example can't prove that. JS engines are built by following the specs, please read the specs page CertainPerformance has linked, there's your evidence (items 8 and 9 in the list). – Teemu Mar 10 '20 at 07:18
  • ok well then i caught another weird behavior in javaScript looking at @YosefTukachinsky's example i wanna know between a string and a boolean datatype which one is converted to which (true == 'true') so as you see i compare the string with boolean true now i wanna know whether to what the boolean type is converted to. is it a number ? – Kevin Mar 10 '20 at 07:33
  • the boolean converted to number. then it compare string to number. string trying to convert to number, and getting `NaN`. so its return `false` – Yosef Tukachinsky Mar 10 '20 at 07:36
  • if you try `true == '1'` you get `true`. because both converted to number, and you get `1==1` which is true – Yosef Tukachinsky Mar 10 '20 at 07:38
  • so the string is converted to NaN when trying to convert itself to a number – Kevin Mar 10 '20 at 07:44
  • if it cannot be converted - yes. if it can be converted - its converted to a number – Yosef Tukachinsky Mar 10 '20 at 10:15
  • if both of types cannot be converted to number (i.e. "abc" == {a: 1}), they both converted to NaN, and `NaN != NaN` (you welcome to chek) – Yosef Tukachinsky Mar 10 '20 at 10:17

0 Answers0