How to Compare Strings in JavaScript

To compare strings in JavaScript, you can use the comparison operators(==, ===, !=, !==, <, >, <=, >=)” or the “localeCompare()” function.

Method 1: Using the comparison operators

Using the standard comparison operators, you can compare strings, such as ==, ===, !=, !==, <, >, <=, and >= in JavaScript. These operators compare strings based on their Unicode character values, performing a lexicographic (dictionary-like) comparison.

Example

const str1 = 'apple';
const str2 = 'banana';
const str3 = 'apple';

console.log(str1 == str2);
console.log(str1 === str3);
console.log(str1 != str2);
console.log(str1 !== str3);
console.log(str1 < str2);
console.log(str1 > str2);
console.log(str1 <= str3);
console.log(str1 >= str3);

Output

false
true
true
false
true
false
true
true

== (Equality): It compares two strings for equal values, returning true if they are equal and false otherwise. If the operands are not the same type, JavaScript will perform type coercion before comparing them. This can sometimes lead to unexpected results, so using === for string comparisons is generally recommended.

=== (Strict Equality): It compares two strings for equal values and equal types, returning true if they are equal and false otherwise. This operator does not perform type coercion, so it’s safer and more predictable than ==.

<, >, <=, >= (Less Than, Greater Than, Less Than or Equal, Greater Than or Equal): These operators compare two strings lexicographically, character by character, based on the Unicode values of their characters. If one string is a prefix of the other, the shorter string is considered “less than”, the longer one.

Remember that these comparisons are case-sensitive, so ‘Apple’ and ‘apple’ would be considered different. If you need to perform a case-insensitive comparison, you can convert both strings to lowercase (or uppercase) using the toLowerCase() (or toUpperCase()) method before comparing them.

Method 2: Using the localeCompare() function

The localeCompare() method compares two strings based on the current or specified locale, considering language-specific rules for character ordering. This is particularly useful when comparing strings with diacritical marks, special characters, or different letter cases.

Example

const str1 = 'apple';
const str2 = 'banana';
const str3 = 'Apple';

// Case-sensitive comparison
console.log(str1.localeCompare(str2));
console.log(str2.localeCompare(str1));
console.log(str1.localeCompare(str3));

// Case-insensitive comparison
console.log(str1.localeCompare(str3, undefined, { sensitivity: 'base' }));

Output

-1
1
-1
0

In the example above, we first performed a case-sensitive comparison between str1, str2, and str3. Then, we performed a case-insensitive comparison between str1 and str3 by specifying the sensitivity option as ‘base’.

Note that the localeCompare() method may have different results in different environments or for different locales, as it considers the specific character ordering rules of the current or specified locale.

Leave a Comment