Can i compare strings in javascript?

You may want to compare two strings to know which is higher or lower alphabetically or to see if they are equal.

You can do this in many ways. I'll show you two of them in this article.

1. How to Compare Strings Using localeCompare

You can use the localeCompare method to compare two strings in the current locale. Here's the syntax:

string1.localeCompare(string2)

locaelCompare returns:

  • 1 if string1 is greater (higher in the alphabetical order) than string2
  • -1 if string1 is smaller (lower in the alphabetical order) than string2
  • 0 if string1 and string2 are equal in the alphabetical order

Here are some examples comparing two strings:

const string1 = "hello"
const string2 = "world"

const compareValue = string1.localeCompare(string2)
// -1

It gives -1 because, in the English locale, h in hello comes before w in the world (w is further down in the alphabetical order than h)

Another example:

const string1 = "banana"
const string2 = "back"

const compareValue = string1.localeCompare(string2)
// 1

The comparison above gives 1 because, in the English locale, ban in banana comes after bac in back.

One more example:

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

const compareValue1 = string1.localeCompare(string2)
// 0

const compareValue2 = string1.localeCompare(string3)
// -1

Comparing "fcc" and "fcc" gives 0 because they are equal in order. "fcc" and "Fcc" gives -1 because capital "F" is greater than small "f".

In some browsers, instead of -1, it may return -2 or some other negative value. So, do not depend on -1 or 1, instead on negative (less than 0) or positive (more than 0) values

2. How to Compare Strings Using Mathematical Operators

You can also use mathematical operators like greater than (>), less than (<), and equal to when comparing strings.

Mathematical operators work similarly to localeCompare – by returning results based on the order of the characters in the string.

Using the previous examples:

const string1 = "hello"
const string2 = "world"

console.log(string1 > string2)
// false

string1 is not greater than string2, because h comes before w, so it is less than.

For the other example:

const string1 = "banana"
const string2 = "back"

console.log(string1 > string2)
// true

string1 is greater than string2 because ban comes after back.

And for the last example:

const string1 = "fcc"
const string2 = "fcc"
const string3 = "Fcc"

console.log(string1 === string2)
// true

console.log(string1 < string3)
// false

string1 is equal to (===) string2, but string1 is not less than string3, which is in contrast to localeCompare.

With mathematical operators, "fcc" is greater than "Fcc", but with localeCompare, "fcc".localeCompare("Fcc")" returns -1 to show that "fcc" is less than "Fcc".

This behavior is one reason why I don't recommend using mathematical operators for comparing strings, even though it has the potential to do so.

Another reason why I don't recommend using mathematical operators is because "fcc" > "fcc" and "fcc" < "fcc" is false. "fcc" is equal to "fcc". So if you're depending on mathematical operators, getting false may be for different reasons than you believe.

So, for comparing strings, amongst the many ways there may be, using localCompare is an effective approach because it can be used for different languages.

Now you know an easy way to compare strings. Happy coding!

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

In this short JS tutorial, you’ll learn how to compare strings and see code examples.

Strict equality

To determine whether the strings are equal, you can use the strict equality operator ===. It returns false if the strings are different and true, if they’re the same

const s1 = 'learn';
const s2 = 'today';

console.log(s1 === 'learn');  // true
console.log(s1 === s2);       // false

Comparing the strings using strict equality === always analyzes the case of the letters, meaning that capital letters are different from the small ones.

const s1 = 'javascript';
const s2 = 'Javascript';

console.log(s1 === s2); // false

Case-insensitive string comparison

If you want to do case insensitive comparison of the strings in JavaScript, you can turn both strings to lowercase and compare them using strict equality operator afterwards.

const s1 = 'javascript';
const s2 = 'Javascript';

console.log(s1.toLowerCase() === s2.toLowerCase()); // true

Comparing the length of JavaScript strings

If you need to find which of two strings is longer, then the operators “greater than” and “lower than” won’t suit you well. They compare the characters of a string in alphanumeric order one by one and consider the length of the strings in the very end.

const s1 = 'javascript';
const s2 = 'node.js';

console.log(s1 > s2); // false

In JS, every string has the length property. By comparing the value of this property in different strings, we’ll get to know which of them is longer.

const s1 = 'javascript';
const s2 = 'node.js';

console.log(s1.length > s2.length); // true

Check if a string contains another string

To check if one string is a substring of another one in JavaScript, there’s a built-in function includes. Remember, the function contains exists in Java, but in JavaScript, it’s deprecated and replaced by includes.

const s1 = 'javascript';
const s2 = 'python';

console.log(s1.includes('script')); // true
console.log(s2.includes('script')); // false
console.log(s1.contains('java'))    // ERROR! .contains is not a function

Read more JavaScript tutorials or Learn Full-Stack JS from scratch!

Can you compare strings with == in JavaScript?

Firstly, you are safe to compare strings that contain characters from Basic Multilangual Plane (including the ASCII characters) using regular comparison operators === , == or utility function Object.is() . Both str1 and str2 contain ASCII characters, so you can safely compare them using comparison operators.

Can === be used for string comparison?

Yes, === can compare two strings, giving true or false as a result.

How do you check if a string is equal to another string in JavaScript?

To check if two strings are equal in JavaScript, use equal-to operator == and pass the two strings as operands. The equal-to operator returns a boolean value of true if the two strings are equal, or else, it returns false.

Can I compare two strings?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.