Solve - equalsIgnoreCase is not a function in JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 20, 2021

banner

Check out my new book

Solve - equalsIgnoreCase is not a function #

The "equalsIgnoreCase is not a function" error occurs because there isn't an equalsIgnoreCase function in JavaScript. To compare two strings ignoring their case, convert both of the strings to lowercase using the toLowerCase() method and do a strict equality check.

typeerror equalsignorecase is not a function

Here is an example of how the error occurs.

index.js
const str1 = 'hello'; const str2 = 'HELLO'; // ⛔️ TypeError: equalsIgnoreCase is not a function const result = str1.equalsIgnoreCase(str2);

To solve the error, use the String.toLowerCase() method to convert both of the strings to lowercase before comparing them.

index.js
const str1 = 'hello'; const str2 = 'HELLO'; const result = str1.toLowerCase() === str2.toLowerCase(); console.log(result); // 👉️ true
By converting both of the strings to lowercase, we are able to do a case insensitive string comparison.

We used the strict equality (===) operator, which returns a boolean result indicating whether the values to the left and right hand-side are equal and of the same type.

Note that the toLowerCase method does not change the original string, it returns a new string. Strings are immutable in JavaScript.

You could achieve the same result by converting both of the strings to uppercase using the String.toUpperCase() method before comparing them.

index.js
const str1 = 'hello'; const str2 = 'HELLO'; const result = str1.toUpperCase() === str2.toUpperCase(); console.log(result); // 👉️ true

You could also add a method named equalsIgnoreCase on the prototype of the String object, however this is discouraged because it is confusing for others when reading your code.

If the strings contain any special characters, e.g. punctuation or digits, they won't be affected by the toUpperCase and toLowerCase methods.

Use the search field on my Home Page to filter through my more than 3,000 articles.