JavaScript Array Methods: A Beginner's Guide to includes()

Checking if a value is in a JavaScript array has never been easier.

Β·

4 min read

The includes() method helps check whether an array contains a specific value. It is a simple and efficient way to validate if a value is in an array and can be particularly useful when working with large or complex data sets.

To use the includes() method, you call it on the array you want to search and pass in the value you are looking for as an argument. If the value is in the array, the includes() method will return a boolean value of true. If the value is not in the array, the includes() method will return false.

Originally published on ByRayRay.dev.

divider-byrayray.png

TLDR;

For those that don't want to deep-dive into how to use the includes() method, here is the TLDR; πŸ˜‰

  • The includes() method is used to check if a value is in an array.

  • It returns a boolean value of true if the value is in the array and false if it is not.

  • You can pass in a second argument as the starting index to search for the value within a specific array range.

  • The includes() method is case-sensitive, so searching for an value with a different capitalization than it appears in the array will return false.

divider-byrayray.png

Search for a value in a JavaScript array

Let's dive into the code and see how to use the includes() method in JavaScript. Here is an example of how to use the includes() method:

%[runkit.com/devbyrayray/check-if-string-is-i..

divider-byrayray.png

Search for a value in a part of a JavaScript array

You can also use the includes() method to search for something inside a part of the array. To do this, you can give the includes() method a second number that tells it where to start looking. For example:

Remember that the includes() method is picky about capital letters. If you want something spelled differently with capital letters, the includes() method will return false.

Find a value while ignoring the case sensitivity

There is an easy way to make the includes()method does not care about capital letters. We can use the some() method to look at each thing in the array. For each thing, we can use the toLowerCase()method to make all the letters lowercase. Then we can see if things are the same. For example:"

divider-byrayray.png

Advanced scenario’s for the includes() method

I wrote two real-world scenarios where the includes() method could be helpful. I hope you can use them for your situation.

Filtering a list of email addresses

Imagine you have a list of email addresses you want to filter based on whether they contain a specific domain. You can use the includes() method to check if each email address contains the domain you are looking for and then add it to a new list if it does. For example:

Validating user input

You have a form on your website where users can enter their zip codes. You can use the includes() method to check if the user's zip code is in a list of valid zip codes for your delivery area. For example:

So whether you're trying to find a specific fruit in a list of fruits, check if someone's zip code is in the right place, or see which number appears the most in a list of numbers, the includes() the method can help you out. Remember to be careful about capital letters (unlike the includes() method) when you use it, and you'll be all set! Happy coding!

divider-byrayray.png

Thanks!

hashnode-footer.png

After reading this story, I hope you learned something new or are inspired to create something new! πŸ€— If I left you with questions or something to say as a response, scroll down and type me a message, send me a DM on Twitter @DevByRayRay

Want to receive new posts in your mailbox? No, not only a link, just the whole article without any ads πŸ€— or other stuff. Then subscribe to my newsletter πŸ‘. I promise I won’t spam you, only the most important and best-quality content will be sent to you ✌️.

Did you know that you can create a Developer blog like this one, yourself? It's entirely for free. πŸ‘πŸ’°πŸŽ‰πŸ₯³πŸ”₯

Did you find this article valuable?

Support Dev By RayRay by becoming a sponsor. Any amount is appreciated!

Β