The more you learn about JavaScript, the more you realize you need to learn. Since you’re here, I take it you want to learn to check if an array-type object contains a given value. So let’s not waste any time and help you find out.
There’s more than one way to check if an array contains a value in JavaScript. Generally, you can use the includes()
and some()
methods, which will return boolean values, or indexOf()
and findIndex()
, which will return the index of the first occurence of the value found.
Method | Description | Use Case |
---|---|---|
includes() | Returns a boolean indicating whether an array includes an item. | Simple and straightforward use case, when you just need to know if a value is present in the array or not. |
indexOf() | Returns the first index at which a given item can be found in the array, or -1 if it’s not present. | When you need to know both the presence and the index of the first occurrence of a value in the array. |
findIndex() | Returns the index of the first item in the array that satisfies the provided testing function, or -1 if no elements pass the test. | When you need to find the index of a value based on a specific condition. |
some() | Returns a boolean indicating whether at least one item in the array passes the test implemented by the provided function. | When you need to know if any value in the array satisfies a specific condition. |
Now let’s dive deeper into each of these methods, with code snippets, to understand how they work and what you need to to make use of them.
With the includes() Method
The easiest, most straightforward way to check if an array contains a value is to use the includes()
method. This method will return a boolean value indicating whether an array includes a certain item. In other words, you’ll either get a true
or a false
.
const fruits = ["apples", "bananas", "oranges"];
console.log(fruits.includes("apples")) // true
With the indexOf() Method
The indexOf()
method will return the index at which the first occurence of a given item can be found in the array, or -1
if it’s not there.
const fruits = ["apples", "bananas", "oranges"];
console.log(fruits.indexOf("apples")); // 0
You can also check if the value isn’t equal to -1
, which will tell you that the item actually exists in the array:
const fruits = ["apples", "bananas", "oranges"];
console.log(fruits.indexOf("apples") !== -1); // true
With the findIndex() Method
The findIndex()
method returns the index of the first element in the array that satisfies a testing function. If it finds an item that satisfies the testing function, it will return its index. Otherwise, it will return -1
.
const fruits = ["apples", "bananas", "oranges"];
console.log(fruits.findIndex(fruit => fruit === "apples")); // 0
Here too, you can do things differently and check if the index isn’t -1
, which will mean that the given value exists in the array:
const fruits = ["apples", "bananas", "oranges"];
console.log(fruits.findIndex(fruit => fruit === "apples") !== -1); // true
N.B. In case you’re wondering, the difference between the indexOf()
and the findIndex()
method is that one expects a value as an argument, and the other a callback function.
With the some() Method
The some()
method returns a boolean value indicating whether at least one element in the array passes the test implemented by the provided function.
const fruits = ["apples", "bananas", "oranges"];
console.log(fruits.some(fruit => fruit === "apples")); // true
In Summary
Thanks for reading this far! I hope this tutorial helped you figure out how to check for a given value in an array. If you have any questions, or would like to share other methods with the rest of this post’s readers, leave a comment below.