I saw in Crockford’s Book Javascript: The Good Parts that he does typeof comparison like this:
return typeof a < typeof b ? -1 : 1;
I made my own tests and I think this is the “ordering” of the different types:
function < number < object or array < string < undefined
Is this how JS actually does the comparison?
The
typeofoperator returns a string. String are compared by its numeric value.So, the
<comparison order would be:tfnosuxare the first characters of the types. ThecharCodeAtmethod returns the numeric charCode of a character in JavaScript.I have added an example of each type at the previous block. Most JavaScript developers know about the first types. The final type,
xml, is less commonly known, and can be obtained by usingtypeofon EX4.Demo of
typeof: http://jsfiddle.net/9G9zt/1/