Why string '10' is less than '9' in JavaScript
Beginner · 4 min read
Ask JavaScript whether '10' < '9' and it says true. That looks wrong, and it causes real bugs: price lists sorted as 1, 10, 100, 2, 20, and "is this bigger?" checks that fail once numbers reach two digits. The comparison itself is working correctly. It is comparing text, and text has its own rules.
In this tutorial you will learn how JavaScript checks two strings for equality and for order, see exactly why '10' comes before '9', and learn how to compare the thing you actually meant. You only need to know what a string and a number are, and what === and < look like.
Equality: same characters, same string
Two strings are equal with === when they contain exactly the same characters in the same order. It doesn't matter how each string was made. 'la' + 'gos' and 'lagos' are equal.
"Exactly the same" really means exactly. A capital letter or a trailing space makes them different.
Order: one character at a time
< and > on two strings compare them like a dictionary does. Look at the first character of each. If they differ, that decides the answer. If they are the same, move on to the second character, and so on.
Each character is compared by its code unit, the number JavaScript uses to store it. Digits 0 to 9 are 48 to 57, capitals A to Z are 65 to 90, and lowercase a to z are 97 to 122.
Here is the exercise. Try it yourself first if you like, or read on and we will build it step by step.
51 Comparing strings, and why 10 is less than 9
easyCompare strings for equality and for order. Explain why '10' < '9' is true.
Stuck? Three hints, smallest first
- 1. Strings have value semantics, so
===compares contents, not identity. - 2.
<and>compare code unit by code unit, stopping at the first difference. - 3. Between a string and a number,
==converts and===does not.
Step 1: equality
console.log('lagos' === 'lagos'); // true
console.log('la' + 'gos' === 'lagos'); // true
console.log('Lagos' === 'lagos'); // false
console.log('lagos ' === 'lagos'); // false
Only the characters matter. A capital L or one extra space is enough to make two strings different. If your data comes from a form, trim() it before comparing.
Step 2: dictionary order
console.log('apple' < 'banana'); // true
console.log('lag' < 'lagos'); // true
console.log('' < 'a'); // true
a comes before b, so apple comes first. lag and lagos match for three characters, then lag runs out, and a shorter string that is a prefix comes first. The empty string comes before everything.
Step 3: why '10' is less than '9'
console.log('10' < '9'); // true
console.log(10 < 9); // false
As strings, JavaScript compares the first characters: 1 against 9. 1 comes first, so the answer is decided right there. It never looks at the 0. As numbers, ten is bigger than nine, so 10 < 9 is false.
Nothing is broken. The values are the wrong type. This happens whenever numbers arrive as text: from a form field, a CSV file, a URL or JSON. It looks right for single digits, which is how it slips through testing.
Step 4: capitals come first
console.log('Z' < 'a'); // true
Every capital letter has a smaller code than every lowercase letter, so Z comes before a. That is why a plain sort puts Zoe before ada. Exercise 45 shows how localeCompare sorts the way a reader expects.
Step 5: == against ===
When one side is a string and the other a number, the two equality operators behave differently:
console.log('10' == 10); // true
console.log('10' === 10); // false
== converts the string to a number first, then compares, so it says true. === never converts: a string and a number are different types, so it says false. Prefer ===, and convert on purpose when you need to.
Step 6: compare what you meant
const a = '10';
const b = '9';
console.log(Number(a) < Number(b)); // false
console.log(['10', '9', '1'].sort()); // [ '1', '10', '9' ]
console.log(['10', '9', '1'].sort((x, y) => x - y)); // [ '1', '9', '10' ]
Number() turns the text into numbers, and then < gives the answer you expected. For sorting, a compare function (x, y) => x - y subtracts the two values. The - operator converts strings to numbers, so the list is ordered by value.
The full solution
The panel prints each comparison next to its expression, including '10' < '9' and 10 < 9. Press Run and read the results, then read the checks underneath.
console.log("'la' + 'gos' === 'lagos' :", 'la' + 'gos' === 'lagos');
console.log("'apple' < 'banana' :", 'apple' < 'banana');
console.log("'10' < '9' :", '10' < '9', "<- '1' comes before '9'");
console.log('10 < 9 :', 10 < 9);
console.log("'10' == 10 :", '10' == 10, " '10' === 10 :", '10' === 10);
Output
12 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 12 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('two identical strings are equal', 'lagos' === 'lagos', true);
test('there is no separate identity to worry about', 'la' + 'gos' === 'lagos', true);
test('case matters', 'Lagos' === 'lagos', false);
test('a trailing space matters', 'lagos ' === 'lagos', false);
test('< compares code unit by code unit, like a dictionary', 'apple' < 'banana', true);
test("so '10' is less than '9', because '1' sorts before '9'", '10' < '9', true);
test('while the numbers compare the other way', 10 < 9, false);
test('== between a string and a number converts the string', '10' == 10, true);
test('=== does not, so it is false', '10' === 10, false);
test('a prefix sorts before the longer string', 'lag' < 'lagos', true);
test('and the empty string sorts before everything', '' < 'a', true);
test('every capital sorts before every lower-case letter', 'Z' < 'a', true);
'10' < '9' is the bug behind most mis-sorted lists: the comparison is correct, the values are the wrong type. It appears whenever numbers arrive as text — from a form, a CSV, a URL, a JSON field that should have been a number — and it looks right for single digits, which is how it survives testing.
'Z' < 'a' is the same mechanism, and it is why a default sort separates the cases. Both are fixed by comparing the thing you actually meant: numbers with a - b, text with a collator.
Common mistakes
- Comparing numbers that are still strings.
'10' < '9'istrue. Convert withNumber()first. - Using
==to "fix" it. It hides the type problem and has its own surprises. Use===and convert on purpose. - Expecting
<to ignore case.'Z' < 'a'istrue. Lowercase both sides or uselocaleCompare.
Try it yourself
- Predict, then check:
'2' > '12','abc' < 'abd'and'B' < 'a'. - A form gives you
'7'and'12'as text. WriteisBigger(a, b)that compares them as numbers. - Sort
['100', '25', '3']as text and as numbers, and explain each result.