Skip to content Skip to sidebar Skip to footer

Javascript Number String Comparision

If we are comparing number string in javascript between 50 to 100 with the higher value its giving false, all other case its giving true. example, '50'<'500' or '99'<'500' i

Solution 1:

strings are compared alphabetically in JavaScript, so '5433' > '111111111111111111' is true because 5 comes later in alphabet than 1

Solution 2:

String comparison returns a value according to the alphabetical order of two compared strings. Since "30" comes before "500" alphabetically, "30"<"500" gives true. Similarly, "99"<"500" gives false because "99" comes after "500".

Solution 3:

Is it because you compare them in alphabetical order. It does not matter how long the word is. "5" is smaller that "9" so "9" will be bigger even than "555555555555555555555".

You have to convert these strings to numbers. After that 9 will be smaller than 55.

I think is something like that:

var mynumber = parseInt('77')

Post a Comment for "Javascript Number String Comparision"