EN
JavaScript - case insensitive string comparison
3 points
In this article, we would like to show you how to do case-insensitive string comparison in JavaScript.
Quick solution:
xxxxxxxxxx
1
var text1 = 'ABC';
2
var text2 = 'abc';
3
4
var result = text1.toUpperCase() === text2.toUpperCase();
5
6
console.log(result); // true
Note:
You can also use
toLowerCase()
method instead oftoUpperCase()
.
In this example, we create a reusable function using modern JavaScript syntax for case-insensitive string comparison.
xxxxxxxxxx
1
const areEqual = (string1, string2) => {
2
return string1.toUpperCase() === string2.toUpperCase();
3
};
4
5
6
// Usage example:
7
8
const text1 = 'ABC';
9
const text2 = 'abc';
10
const text3 = 'AbC';
11
12
console.log(areEqual(text1, text2)); // true
13
console.log(areEqual(text1, text3)); // true
14
console.log(areEqual(text2, text3)); // true