Sometimes, we want to use JavaScript includes() in a case insensitive manner.
In this article, we’ll look at how to use JavaScript includes() in a case insensitive manner.
How to use JavaScript includes() in a case insensitive manner?
To use JavaScript includes() in a case insensitive manner, we can convert all strings to the same case.
For instance, we write
const arr = ["foo", "bar", "bar"];
const lookup = "bAr";
console.log(arr.some((x) => x.toLowerCase() === lookup.toLowerCase()));
to call arr.some with a callback that converts the lookup string and the arr entry string x to lower case with toLowerCase.
And then we compare them to see if they’re equal in the some callback.
some returns true if any value in arr matches the condition we return in the callback.
Conclusion
To use JavaScript includes() in a case insensitive manner, we can convert all strings to the same case.
