Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  198] [ 5]  / answers: 1 / hits: 9110  / 10 Years ago, thu, august 7, 2014, 12:00:00

I found these examples in another thread but I don't get it.


0 == '0'            // true

0 to the left I converted to false(the only number that does that). The right is a non empty string which converts to true.
So how can
false == true --> true


What have I missed?


More From » javascript

 Answers
1

Here is an official answer to your question (the quoted parts, link at the bottom) and analysis:



Equality (==)




The equality operator converts the operands if they are not of the same type, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
Syntax



x == y



Examples



3 == 3 // true



3 == 3 // true



3 == '3' // true




This means, as I read it, that the first 3 (integer) is converted to string to satisfy the comparison, so it becomes '3' == '3', which is true, same as in your case with zeroes.



NOTE: I assume that the converion may vary in different JS engines, although they have to be unified under ECMAScript specifiction - http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 (quoted @Derek朕會功夫). This assumption is made on a subjective and imperative opinion that not all browsers and JavaScript engines out there are ECMAScript compliant.



Identity / strict equality (===)




The identity operator returns true if the operands are strictly equal (see above) with no type conversion.




The Identity / strict equality (===) found on the same resource at the end of the answer will skip the automatic type conversion (as written above) and will perform type checking as well, to ensure that we have exact match, i.e. the expression above will fail on something like:



typeof(int) == typeof(string)



This is common operator in most languages with weak typing:



http://en.wikipedia.org/wiki/Strong_and_weak_typing



I would say that one should be certain what a function/method will return, if such function/method is about to return numbers (integers/floating point numbers) it should stick to that to the very end, otherwise opposite practices may cut your head off by many reasons and one lays in this question as well.



The above is valid for other languages with weak typing, too, like PHP for example.



Read more of this, refer second head (Equality operators):



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators


[#43272] Wednesday, August 6, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
randall

Total Points: 492
Total Questions: 99
Total Answers: 103

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
randall questions
Wed, Mar 16, 22, 00:00, 2 Years ago
Tue, Nov 10, 20, 00:00, 4 Years ago
Sat, Oct 31, 20, 00:00, 4 Years ago
;