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.

Al Lu
UX Design Techdegree Student 15,698 PointsHow to check if string has same content
Hey. What is the best way to check if a string has the same content? so if its has it will do one thing, if its not, it will do something else. I know how to use if or else, but about the comparison itself. Is there a string method for this?
2 Answers

Tsenko Aleksiev
3,819 PointsI'm not sure what exactly you want to do but I think that localCompare() method is the best choice. It returns -1 if the reference string is sorted before the compareString, 0 if are the same and 1 if the reference string is sorted after the compareString. Syntax: string.localeCompare(compareString)
var str1 = "cd";
var str2 = "ab";
var n = str1.localeCompare(str2);
1 // str1 is sorted after str2
Keep in mind that strings are compared in sort order and this methods depends on the language settings of the browser.

Barbara Szczygielska
7,535 PointsIn JavaScript you can easly compare two stings with == operator (you should check it out here) Although you should be carefull because == operator checks for lower/uppercase Example:
var first = "a";
var second = "b";
var third = "a";
var fourth = "A";
first == second; // false
first == third; // true
first == fourth; // false
first.toUpperCase() == fourth; // true
So if you want to compare the content, you should use toLowerCase or toUpperCase ;) (MDN toUpperCase)
I hope it helped, cheers! :)