I got these 2 examples below -
console.log("a" > "3")
// outputs true
console.log("hello" > "3")
// outputs true
According to MDN, If both values are strings, they are compared as strings, based on the values of the Unicode code points they contain
.
But then they also wrote the following in the next paragraph, Strings are converted based on the values they contain, and are converted as NaN if they do not contain numeric values.
Following this logic, shouldn't both statements be false
since no matter what the operator it is, "a" and "hello" are words in strings and they don't have a numerical value, therefore, it should return NaN
, and NaN
is false; hence, as soon as one of the operands is false
, it outputs false
?
If I need to adhere to the former statement above, could you please walk me through this logic?
Thanks.
You are comparing two strings (in quotation marks), not a string to a number. This should answer your question:
"a" > "3" //true
"a" > 3 //false