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.
To perform this operation the most preferred method is to use either toUpperCase() or toLowerCase() function.
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.
HTML
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > JavaScript | Case insensitive string comparison </ h3 > < p id = "GFG_up" style = "color:green;" ></ p > < button onclick = "myGeeks()" > Click here </ button > < p id = "GFG_down" style = "color:green;" ></ p > < script > var str1 = "this iS geeksForGeeKs"; var str2 = "This IS GeeksfOrgeeks"; var p_up = document.getElementById("GFG_up"); p_up.innerHTML = str1 + "< br >" + str2; function myGeeks() { var p_down = document.getElementById("GFG_down"); var areEqual = str1.toUpperCase() === str2.toUpperCase(); p_down.innerHTML = areEqual; } </ script > </ body > |
Output:

Case Insensitive strings
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.
HTML
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > JavaScript | Case insensitive string comparison </ h3 > < p id = "GFG_up" style = "color:green;" ></ p > < button onclick = "myGeeks()" > Click here </ button > < p id = "GFG_down" style = "color:green;" ></ p > < script > var str1 = "this iS geeks"; var str2 = "This IS GeeksfOrgeeks"; var p_up = document.getElementById("GFG_up"); p_up.innerHTML = str1 + "< br >" + str2; function myGeeks() { var p_down = document.getElementById("GFG_down"); var areEqual = str1.toLowerCase() === str2.toLowerCase(); p_down.innerHTML = areEqual; } </ script > </ body > |
Output:

Case Insensitive strings
Example: In this example, we will use the localeCompare function to compare two strings.
HTML
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > JavaScript | Case insensitive string comparison </ h3 > < p id = "GFG_up" style = "color:green;" ></ p > < button onclick = "myGeeks()" > Click here </ button > < p id = "GFG_down" style = "color:green;" ></ p > < script > var str1 = "this iS geeks"; var str2 = "This IS GeeksfOrgeeks"; var p_up = document.getElementById("GFG_up"); p_up.innerHTML = str1 + "< br >" + str2; function myGeeks() { var p_down = document.getElementById("GFG_down"); var areEqual = str1.localeCompare(str2, undefined, {sensitivity : 'accent'}); p_down.innerHTML = areEqual === 0 ? true : false; } </ script > </ body > |
Output:

Case Insensitive strings
Example: In this example, we will use the regular expression to compare two strings.
HTML
< body style = "text-align:center;" > < h1 style = "color:green;" > GeeksForGeeks </ h1 > < h3 > JavaScript | Case insensitive string comparison </ h3 > < p id = "GFG_up" style = "color:green;" ></ p > < button onclick = "myGeeks()" > Click here </ button > < p id = "GFG_down" style = "color:green;" ></ p > < script > var str1 = "this iS geeksforGeeks"; var str2 = "This IS GeeksfOrgeeks"; var p_up = document.getElementById("GFG_up"); p_up.innerHTML = str1 + "< br >" + str2; function myGeeks() { var pattern = new RegExp(str1, 'gi'); var result = pattern.test(str2); var p_down = document.getElementById("GFG_down"); p_down.innerHTML = result ? true : false; } </ script > </ body > |
Output:

Case Insensitive strings
Please Login to comment...