There are two common ways to check if the array contains a string ignoring case in JavaScript: using the array.findIndex() function and using the array.some() function. Let’s see the explanations and examples below to learn how to use these two methods
Ignoring case Check if Array contains String – JavaScript
Using the array.findIndex() function
The JavaScript array.findIndex()
function is used to find the first position of the element found that satisfies the specified condition. To check if the array contains string ignoring cases, we use the array.findIndex()
function together with the string.toLowerCase()
or the string.toUpperCase()
function.
For example:
const arrCars = ["Rolls-Royce", "Maybach", "Lamborghini", "Aston Martin"]; const myCar = "aston martin"; // Check if the array contains string const result = arrCars.findIndex(element => myCar.toLowerCase() === element.toLowerCase()); console.log("The index of the string to be searched is: " + result);
Output:
The index of the string to be searched is: 3
In the above example, we used the array.findIndex()
function together with the string.toLowerCase()
function to find the position of string myCar in arrCars array. Since the string myCar = “aston martin” is the third in the array, the function returned the index of 3. In case we can’t find the matching string, the array.findIndex()
function will return -1.
In addition, we can use the string.toUpperCase()
function together with the array.findIndex()
function to produce the same result.
Using the array.some() function
The array.some()
function in JavaScript is used to check whether the elements in the array satisfy certain conditions. We can use the array.some()
function in combination with the string.toLowerCase()
or the string.toUpperCase()
function to check if the array contains the string ignoring cases in JavaScript.
For example:
const arrCars = ["Rolls-Royce", "Maybach", "Lamborghini", "Aston Martin"]; const myCar = "aston martin"; // Check if the array contains string const result = arrCars.some(element => myCar.toLowerCase() === element.toLowerCase()); console.log("The array containing the desired string: " + result);
Output:
The array containing the desired string: true
In the above example, the string myCar = “aston martin” appears in the array arrCars
, so the array.some()
returns “true”. If the search string is not found, the array.some() function will return “false”.
The primary difference between the array.some()
function and the array.findIndex()
function is that the array.some()
function only checks if the array contains a matching string and returns either “true” or “false”. While the array.findIndex()
function will check if the array contains a string and return the exact index of that string in the array.
Summary
To conclude, we have learned how to check if the array contains string ignoring cases in JavaScript in two methods, and both ways give the desired result. However, using the array.findIndex()
function is more convenient because this way not only checks if the string exists in the array but also gives the position of that string in the array, so we recommend you use this way in your project.

Hi, I’m Mary Ralston. I have experience programming in HTML, javascript, C++,… and I want to share with you my experience. So if you want to know more about the above languages read my articles