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

Robert Rydlewski
Robert Rydlewski
3,828 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
231,248 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,828 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
231,248 Points

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

thanks for that duke