How to uselocalCompare() in Javascript
The localeCompare() method in JavaScript compares strings based on the current locale, returning a value indicating their relative order.
Syntax:
referenceString.localeCompare(compareString);
Example: In this example, we will use the localeCompare function to compare two strings.
let str1 = "this iS geeks";
let str2 = "This IS GeeksfOrgeeks";
console.log(str1)
console.log(str2)
function myGeeks() {
let areEqual = str1.localeCompare(str2, undefined, { sensitivity: 'accent' });
console.log(areEqual === 0 ? true : false);
}
myGeeks()
Output
this iS geeks This IS GeeksfOrgeeks false
Compare the Case Insensitive strings in JavaScript
Comparing strings in a case-insensitive manner means comparing them without taking care of the uppercase and lowercase letters.
Here are some common approaches to compare the case-insensitive string in JavaScript:
Table of Content
- JavaScript toUpperCase() function
- JavaScript toLowerCase() function
- Using localCompare()
- Using regular expression