Compare Two Strings in JavaScript (7 Programs)
Comparing two strings in JavaScript is a common requirement in various applications, such as user authentication, sorting, and validation. Understanding the different methods to compare strings in JavaScript ensures accurate results, considering case sensitivity and encoding differences.
This guide explores multiple ways to compare two strings in JavaScript, helping developers choose the most suitable approach for their needs.
Compare Two Strings in JavaScript Using Equality Operators
The simplest way to compare strings is by using equality operators (== or ===).
Code
const str1 = "hello";
const str2 = "hello";
const str3 = "Hello";
console.log(str1 === str2); // true
console.log(str1 === str3); // false
console.log(str1 == str3); // false
Output
true
false
false
Explanation
-
The === operator checks both value and type.
-
The == operator performs type coercion but is case-sensitive.
Compare Two Strings in JavaScript Using localeCompare() Method
The localeCompare() method provides a locale-sensitive string comparison.
Code
const str1 = "apple";
const str2 = "banana";
console.log(str1.localeCompare(str2)); // -1 (apple comes before banana)
console.log(str2.localeCompare(str1)); // 1 (banana comes after apple)
console.log(str1.localeCompare(str1)); // 0 (both are equal)
Output
-1
1
0
Explanation
-
Returns -1 if the first string comes before the second.
-
Returns 1 if the first string comes after the second.
-
Returns 0 if both strings are equal.
Case-Insensitive String Comparison in JavaScript
To perform a case-insensitive comparison, convert both strings to the same case before comparing.
Code
const str1 = "Hello";
const str2 = "hello";
console.log(str1.toLowerCase() === str2.toLowerCase()); // true
Output
true
Explanation
The toLowerCase() method converts both strings to lowercase before comparing them.
Comparing Two Strings in JavaScript Using toUpperCase()
Using toUpperCase() ensures both strings are converted to uppercase for comparison.
Code
const str1 = "Hello";
const str2 = "HELLO";
console.log(str1.toUpperCase() === str2.toUpperCase()); // true
Output
true
Explanation
The toUpperCase() method converts both strings to uppercase before comparison.
JS String Comparison Using Regular Expressions
Regular expressions can be used to compare strings flexibly.
Code
const str1 = "hello";
const str2 = "HELLO";
const regex = new RegExp(str1, "i");
console.log(regex.test(str2)); // true
Output
true
Explanation
The RegExp object with the i flag performs a case-insensitive comparison.
Compare Two Strings in JavaScript Using Lexicographic **
JavaScript compares strings lexicographically (based on Unicode values).
Code
console.log("apple" < "banana"); // true (lexicographically 'a' < 'b')
console.log("grape" > "apple"); // true ('g' > 'a')
Output
true
true
Explanation
The comparison is based on Unicode values of characters.
Character-by-Character String Comparison in JavaScript
To compare strings manually, loop through each character.
Code
function compareCharacterByCharacter(str1, str2) {
if (str1.length !== str2.length) return false;
for (let i = 0; i < str1.length; i++) {
if (str1[i] !== str2[i]) return false;
}
return true;
}
console.log(compareCharacterByCharacter("test", "test")); // true
console.log(compareCharacterByCharacter("test", "best")); // false
Output
true
false
Explanation
-
The function checks each character individually.
Concepts Used in String Comparisons
Case Sensitivity
-
Strings in JavaScript are case-sensitive by default.
Unicode Value Comparison
-
Strings are compared based on their Unicode values.
Normalization
-
Using toLowerCase() or toUpperCase() ensures consistency during comparisons.
Equality vs. Lexicographic Comparison
-
Use equality operators for direct matching and localeCompare() for sorting.