b; //...">

Why is one line bigger than the other when comparing strings in JavaScript?

I see this code from the book:

var a = "one"; var b = "four"; a>b; // will return true 

but it does not mention why "one" is greater than "four." I tried c = "a" and it is smaller than a and b. I want to know how JavaScript compares these strings.

+29
javascript string compare
Aug 17 2018-11-11T00:
source share
5 answers

Because, as in many programming languages, strings are compared lexicographically .

You can think of it as a more convenient version in alphabetical order , the difference being that letter ordering covers only 26 characters a through z .




This answer is in response to java , but the logic is exactly the same. Another good one: String Compare "Logic" .

+26
Aug 17 '11 at 4:10
source share

β€œone” begins with β€œo”, β€œfour” begins with β€œf”, β€œo” later in the alphabet than β€œf”, so β€œone” is greater than β€œfour”. See this page for some good examples of JavaScript string comparisons (with explanations!).

+7
Aug 17 '11 at 4:10
source share

Javascript uses lexicographic order for the operator . 'f' continues to 'o', so comparing "one"> "four" returns true

+3
Aug 17 '11 at 4:14
source share

Comparison operators are used in logical operators to determine the equality or difference between variables or values.

You can see that each statement:

http://www.w3schools.com/js/js_comparisons.asp

When a string (Text) you should use ==, === or! = When comparing numbers, you can use> =, <=, etc.

0
Oct 28 '15 at 18:46
source share

you need to send them an integer

Here is an example

 if (parseInt($('#myRangeMin').val()) >= parseInt($('#myRangeMax').val())) 
-one
Dec 13 '17 at 7:48
source share



All Articles