Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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??

karson Adam
karson Adam
32,618 Points

("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 ))