4 Best Methods to Search Data in Your JavaScript Arrays
4 Best Methods to Search Data in Your JavaScript Arrays

Photo by Amit Lahav on Unsplash

“What’s the best way to do this?”

This is a question I’m sure you have asked yourself a hundred times using JavaScript. And that’s because this language has so many different ways of doing anything.

If you’re working with arrays, you will often have to search through them. But what’s the best way to do that?

What if you need to know if a value is present in an array? What if you want to filter something out of your collections?

Turns out that in this sea of information, JavaScript has 4 amazing methods to do anything you may need when searching through your arrays.

Let’s start.

Use includes()

The includes() method returns a boolean telling you if the passed-in parameter exists or not in an array.

In my example, I’ve used this method to ask the question: “Hey array, do you contain this value, yes or no?”

An extra important thing to note about this method is that it doesn’t implicitly perform type coercion on the value you pass in. So basically, it won’t modify the type of parameter you pass in to confront values.

As you can see, a strict comparison is performed here, so the number 4 won’t be converted to the string '4' to test the condition.
['Piero Borrelli']
['Technology', 'Web Development', 'Programming', 'JavaScript', 'Arrays']