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 Foundations Strings Comparing Strings

Tom Checkley
Tom Checkley
25,165 Points

Why would you ever need to compare a string?

I understand the code in this video, I just don't really know why you'd need to know which string was less than another.

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

One common example would be matching 2 passwords together. If one string is less than the other than clearly those passwords do not match and the second input field needs to be changed. Or maybe the first.

So there's one practical example of a use for string comparisons. :-)

Hi Tom,

A practical example of string comparison is for sorting an array of strings. Another might be if you're asking the user a Yes/No question and want to check what they typed - or more broadly, comparing what the user types to a value of what you may be expecting.

Tom Checkley
Tom Checkley
25,165 Points

Ah, that makes a lot of sense. Thank you both, I find it much easier grasping stuff if I know what its practical application is.

Lucas Stinis
Lucas Stinis
10,706 Points

Tom, you are totally right..

I feel this course is great when you are trying to learn how JS works as a language, but not so much when you are trying to learn how to program in general. I'm a fairly seasoned programmer that is taking this course to brush up on the most basic aspects of JavaScript so I can teach my 2 nephews how to program in a more concise way. As a primer for the rules JS follows it's awesome, but I can imagine it feels a bit theoretical if you you don't have experience with this kind of thing.

If your goal is to learn how to program, I suggest you first do the Python courses Treehouse offers. My feeling is those are more heavy on real-world uses of the language they are teaching you. I might be wrong, but you might want to give it a try. There's also an introduction to programming one I think.