Open In App
Related Articles

Compare the Case Insensitive strings in JavaScript

Improve Article
Improve
Save Article
Save
Like Article
Like

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:

Approach 1: JavaScript toUpperCase() function:

The str.toUpperCase() function converts the entire string to Upper case. This function does not affect any of the special characters, digits, and alphabets that are already in upper case. 

Syntax:

string.toUpperCase()

Example: This example uses toUpperCase() function to compare two strings. 

Javascript




let str1 = "this iS geeksForGeeKs";
let str2 = "This IS GeeksfOrgeeks";
console.log(str1)
console.log(str2)
 
function myGeeks() {
    let areEqual = str1.toUpperCase() === str2.toUpperCase();
    console.log(areEqual);
}
myGeeks()


Output

this iS geeksForGeeKs
This IS GeeksfOrgeeks
true

Approach 2: JavaScript toLowerCase() function:

The str.toLowerCase() function converts the entire string to lower case. This function does not affect any of the special characters, digits, and alphabets that are already in lowercase. 

Syntax:

string.toLowerCase()

Example: This example uses the toLowerCase() function to compare two strings. 

Javascript




let str1 = "this iS geeks";
let str2 = "This IS GeeksfOrgeeks";
console.log(str1)
console.log(str2)
 
function myGeeks() {
    let areEqual = str1.toLowerCase() === str2.toLowerCase();
    console.log(areEqual);
}
myGeeks()


Output

this iS geeks
This IS GeeksfOrgeeks
false

Approach 3: Using localCompare()

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. 

Javascript




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

Approach 4: Using regular expression:

A regular expression (regex) is a sequence of characters that define a search pattern.

Syntax:

let regex = new RegExp(pattern, flags);

Example: In this example, we will use the regular expression to compare two strings. 

Javascript




let str1 = "this iS geeksforGeeks";
let str2 = "This IS GeeksfOrgeeks";
console.log(str1)
console.log(str2)
 
function myGeeks() {
    let pattern = new RegExp(str1, 'gi');
    let result = pattern.test(str2);
    console.log(result ? true : false);
}
myGeeks()


Output

this iS geeksforGeeks
This IS GeeksfOrgeeks
true


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 Jul, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials