We often encounter situations which we have to compare strings in JavaScript programming. The problems of the operation can be comparing strings based on characters and their values, comparing based on the length of strings, or in alphabetical order. This article will give different solutions for each case.
How to compare strings in JavaScript?
In this article, we divide the problem into three cases:
- First case: compare strings based on values and characters.
- Second case: compare strings based on length.
- Third case: compare strings based on alphabetical order.
Compare strings in JS based on values and characters
The strict equality operator (===) checks if the two input operands are equal and then returns a Boolean result. The strict equality operator is different from the equality operator (==). The strict equality operator checks for both value types of the two operands. So the strict equality operator always treats the operands of two different value types as different.
Example:
const x = 'Learn';
const y = 'LEARN';
console.log(x === y);
Output:
false
If you want to compare two strings case-insensitive, convert both strings to lowercase format using the toLowerCase() method before comparing them.
Syntax:
string.toLowerCase()
Parameter:
None
Example:
const x = 'Learn';
const y = 'LEARN';
console.log(x.toLowerCase() === y.toLowerCase());
Output:
true
Compare strings in JavaScript based on length
The “length” property in JavaScript returns the length of the specified string. We can compare strings in JavaScript based on string length by combining the “length” property with comparison operators.
Example:
const x = 'Learn';
const y = 'LEARN';
console.log(x.length == y.length);
Output:
true
Compare strings in JS based on alphabetical order
The localeCompare() method in JavaScript is used to compare strings in the current locale based on the browser’s locale settings. This method returns “-1”, “1”, or “0” representing the order before, after, and equal in the alphabet.
Syntax:
string1.localeCompare(string2)
Parameter:
string2: String to compare with string1.
The following example declares three strings: string1, string2, and string3. Then we use the localeCompare() method to compare the strings based on alphabetical order.
Example:
var string1 = "Learn";
var string2 = "Share";
var string3 = "IT";
console.log(string1.localeCompare(string3));
console.log(string1.localeCompare(string2));
console.log(string1.localeCompare(string1));
Output:
1
-1
0
The “localCompare()” method returns “1” because string1 = “Learn” is greater than string3 = “IT” in alphabetical order (the L comes after the I).
String2 = “Share” is greater than string1 = “Learn” alphabetically, then the “localCompare()” method returns “-1”.
Finally, the “localCompare()” method will return “0” when both strings are equal.
Summary
This article showed you how to compare strings in JavaScript in different cases. I hope the information in this article was useful to you.
Maybe you are interested:
- Remove all Line Breaks from a String in JavaScript
- Count the Unique Words in a String using JavaScript
- Get the Substring before a specific Character in JavaScript

Hello, my name’s Bruce Warren. You can call me Bruce. I’m interested in programming languages, so I am here to share my knowledge of programming languages with you, especially knowledge of C, C++, Java, JS, PHP.
Name of the university: KMA
Major: ATTT
Programming Languages: C, C++, Java, JS, PHP