Categories
JavaScript Answers

How to make array indexOf() case insensitive with JavaScript?

Spread the love

Sometimes, we want to make array indexOf() case insensitive with JavaScript.

In this article, we’ll look at how to make array indexOf() case insensitive with JavaScript.

How to make array indexOf() case insensitive with JavaScript?

To make array indexOf() case insensitive with JavaScript, we can use the array findIndex method.

For instance, we write

const array = ["I", "hAve", "theSe", "ITEMs"];
const index = array.findIndex((item) => q.toLowerCase() === item.toLowerCase());

to call array.findIndex with a callback that converts the item in array to lower case with toLowerCase.

And we compare it with value q after converting it to lower case.

Then the index of the array item that matches q in a case insensitive manner is returned.

Conclusion

To make array indexOf() case insensitive with JavaScript, we can use the array findIndex method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *