Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Review Conditional Statements and Comparison Operators

Mike Me
Mike Me
5,148 Points

comparison operatings

I'm a bit confused,

why ("apple" > "bear") is true but ("lion" > "zebra") is false??

as I know if letter comes first(alphabetically ) than it's greater. but despite that letter "L" comes before "Z" the condition false. please explain why??

("apple" > "bear") is false

2 Answers

Jacek Szemplinski
Jacek Szemplinski
6,423 Points

Hi Mike,

In most programming languages, including Javascript, strings are compared lexicographically (see this Wikipedia page for some fancy scientific explanation ;).

So, basically, how "apple" > "bear" is evaluated:

  • Let's take first character of each string.

  • 'a' is LESS than 'b' (it lies closer to the beginning of alphabet than b), so whole statement evaluates to false

"lion" > "zebra" example:

  • Let's take first character of each string

  • 'l' is LESS than 'z', so again, whole statement evaluates to false

I don't know why for you "apple" > "bear" evaluated to true, but it could happen if, by a mistake, you added a capital letter instead of lowercase letter in "bear". Lowercase letters are greater than uppercase letters.

Jacek.

Mike Me
Mike Me
5,148 Points

By bad! I did think that a is greater than b which confused and made me ask silly questions, Thanks to you I found it ))