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 Comparison Operators

Robert Rydlewski
Robert Rydlewski
3,827 Points

('apple' < 'bear' ) TRUE ... The author claim this statement is true because a is before b. (2:38)

Can someone explain to me this more in detail? Correct me if I am not right. When we comparing the 2 strings, we are comparing only the first letter of the string not rest of it - right ??? That's what auto said. If I would compare every letter and put value to it as : A = 1 B = 2 C = 3 ..etc The apple would equal to 50 and bear would equal 26. But since apple(50) < bear (26) in this case we only comparing the first letter right ??

What if we have this ('apple < "onion is good for you" ) ??? Will u account every first letter of the 2nd string ??

Please let me know guys. Thanks and happy coding :)

4 Answers

Steven Parker
Steven Parker
216,151 Points

String comparison is done one letter at a time, starting with the first character. If they are different, the comparison is satisfied and the "lesser" string is the one that starts with the letter that comes first in the alphabet.

Only if the first letters are the same, the second character in each string is compared. They are evaluated just as the first was. The comparison keeps moving further along until it encounters two characters that are not the same.

So only one letter is examined to determine that 'apple' < 'bear', or that 'apple < "onion is good for you". But all 5 are examined to determine that "abcda" < "abcde".

Robert Rydlewski
Robert Rydlewski
3,827 Points

Thanks for clear this up for me. I appreciate your help :)

hi there

i know that Anna< Beatrice is true

what about Betty > Amy??

Steven Parker
Steven Parker
216,151 Points

Assuming you put quotes around them, that would also be true. Try experimenting with it yourself in the workspace!

thanks for that duke