Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Expressions & Operators  ›  Lesson

Comparison and String Operators

Comparison Operators 12 minComparison operators (> < >= <=) compare numbers by size, but compare strings letter by letter using character codes, so "9" > "80" is true while 9 > 80 is false; + on strings concatenates rather than adds.

Comparison and String Operators

In short: > < >= <= compare numbers by size, the way a balance scale weighs two things against each other. But given two strings, they compare letter by letter using each character's underlying code — "apple" < "banana" is true because 'a' sits before 'b', and the comparison stops at the first letter that differs. This means "9" > "80" is true — as text, '9' outranks '8' — even though the number 9 is smaller than 80. The + operator is also overloaded: between numbers it adds, but with a string on either side it joins text.

A balance scale weighing 8 against 5 (8 > 5 → true); plus "apple" < "banana" (compares 'a' vs 'b', stops at letter 1, true) and "9" > "80" (compares '9' vs '8' as characters, true — not numeric size!)

Weigh two numbers on the scale

For numbers, comparison operators behave exactly as expected — pure size:

javascript

8 > 5 is true — a straightforward size comparison. "apple" < "banana" is also true, but for a different reason entirely: it compares the first character of each string, 'a' against 'b', finds they differ, and stops right there — the rest of each word is never even looked at.

Compare two version numbers as text

This is where string comparison stops matching intuition — it never converts to a number unless you ask it to:

javascript

"9" > "80" is true: both sides are strings, so the comparison checks character by character, and '9' comes after '8' in character order — the comparison never gets to the second character of "80" at all. Change one side to a real number, "9" > 80, and the rule flips: with a number on one side, the string is converted to a number first, giving the expected false (9 is not greater than 80).

Join two strings with plus

+ adds when both sides are numbers, but the moment either side is a string, it switches to concatenation — gluing text together:

javascript

This prints "Blue Hour". Each + here sees a string on at least one side, so it joins rather than adds — the same rule that turns 5 + "5" into "55" in the arithmetic lesson.

Quick practice

quick drill

What is 8 > 5?

quick drill

What is "apple" < "banana"?

quick drill

What is "9" > "80"?

quick drill

What is "9" > 80?

quick drill

What does "Blue" + " " + "Hour" produce?

🎯 Your turn

A settings page compares two version numbers with "9" > "80" to decide which is newer, and gets true. Explain exactly why, and how you would fix the comparison.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSorting version numbers as text

A settings page compares two version numbers with "9" > "80" to decide which is newer, and gets true. Explain exactly why, and how you would fix the comparison.