简体   繁体   中英

Can any one explain me how does the string and number comparison work in javascript?

I have compared some strings and numbers to see the result as true and false

"dfdf" > 1
false
"dfdf" > 99
false
"dfdf" > 9999
false
"dfdf" > 99999
false
1> ''
true
1> '545'
false
1> '545d'
false
1> '555'
false
1> 'ddfdf'
false
9999 > 'dfsdfadf'
false

I have tried few different combinations in the code as you can see but got the mixed result and want to know how exactly the comparison works in javascript.

another addition which is even more confusing

"dasfads" > "dasfdsf"
false
"abc" > "a"
true
"abc" > "agf"
false
"abcf" > "agf"
false

Comparing a string to a number will force the string data to evaluate into a number value. If the string data is not convertible to a numerical value it will return a NaN number to the given comparison.

Since NaN is not comparable nor equal to anything at all, not even to another NaN

NaN == NaN > false

The 'greater than' or 'smaller than' NaN comparison will have to return false both ways. Because that's the only correct answer, nothing can be greater nor smaller than the value you don't have. Therefore both claims are false. eg: 0 > NaN and 0 < NaN > false.

But keep in mind that comparing two strings of data such as: "98A" > "999" will return a comparative false , whereas: "9A" > "999" will return true

Which is a very powerful thing to know, because knowing this (two strings will be compared by alphabetical order of magnitude) you are able to compare time data without taking the burden of converting those values to numbers and directly go with:

"09:32:28" > "09:31:59" > true

And luckily "PM" > "AM" > true by pure (linguistic) chance.

It is attempting to cast the string to a number.

In the console, if you do

x = +''

then x will be set to 0 . 1 > 0 is true .

For all the other ones, if you do something like

x = +'dfdf'

then x will be set to NaN (not a number), and any comparison with it will return false.

There are 2 types of comparison available when using JavaScript,

The standard equality operators (== and !=) use the Abstract Equality Comparison Algorithm to compare two operands. If the operands are of different types, it will attempt to convert them to the same type before making the comparison, eg, in the expression 5 == '5', the string on the right is converted to Number before the comparison is made.

The strict equality operators (=== and !==) use the Strict Equality Comparison Algorithm and are intended for performing equality comparisons on operands of the same type. If the operands are of different types, the result is always false so 5 !== '5'.

So if you want to know what is happening there, JavaScript is trying to convert the string into a Number and then do comparison, however, since they are not a number, which is always converted to NaN, and comparison with NaN will be equal to false.

Link for details:

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

String Vs String

The comparison is done character by character when you are comparing strings. First different character found in both string will be compared using their char codes. (Note: Chars after that will be ignored.)

>> "a".charCodeAt(0)
97
>> "}".charCodeAt(0)
125
>> "}" > "a"
true
>> "-}" > "a"
false

String Vs Number

Any time a string is compared with a number the string is converted into an integer and then both are compared. If the string can't be converted into a number, then it returns false. (Note: NaN, Nothing can be compared against NaN. Any comparison with NaN will return false)

>> 'dfsdfadf' > 999999 // Can't be converted to integer, hence false
false
>> 'd' > 9999999999 // Can't be converted to integer, hence false
false
>> '' == false // '' is empty string which is 0 or false
true
>> 1 > '' // '' is false,0 explained in prev case
true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM