Finding a value in an array is an important task in programming and data analysis. It involves searching for a specific element within a collection of data stored in an array. This is useful when you need to locate a particular value, check for duplicates, sort data or perform statistical calculations.
6 Ways to Check What a JavaScript Array Contains
- IndexOf method
- Includes method
- For loop method
- Some method
- Find method
- FindIndex method
By finding values in an array, you can extract relevant information, determine their positions and solve problems efficiently.
Here’s what you need to know.
JavaScript Array Contains: Find a Value
There are three common ways to check if a primitive value is present in an array. You can use:
- indexOf method
- Includes method
- For loop method
Any of these methods can check if a primitive value is present in an array.
Consider we have this array:
var num = [1,2,3,4,5];
indexOf Method
The indexOf()
method returns the first index of the element in the array, if the searching element is not present in the array then it returns -1
.
num.indexOf(3); // 2
num.indexOf(6); //-1
num.indexOf('3'); // -1
The NaN value is not handled in the indexOf method.
var array = [NaN];
array.indexOf(NaN); // -1
If your array can contain NaN
, then you can use the includes method.
Includes Method
The includes()
method returns true
if the value passed is present in the array. Otherwise, it returns false
.
num.includes(1); // true
num.includes(0); // false
Checking NaN values
var array = [NaN];
array.includes(NaN); // true
For Loop Method
We can write a for
loop to manually check if the element present in the array.
function checkIfElementPresent(array , element) {
for(let i = 0, len = array.length; i < len; i++){
if(array[i] === element){
return true;
}
}
return false
}
JavaScript Array Contains: Search NaN, Undefined and Null
var array = [undefined, NaN, null ];
// undefined
array.indexOf(); // 0
array.indexOf(undefined); // 0
array.includes(); // true
array.includes(undefined); // true
// NaN
array.indexOf(NaN); // -1, indexOf doesn't handled for NaN
array.includes(NaN); // true
// null
array.indexOf(null); // 2
array.includes(null); // true
JavaScript Array Contains: Find an Object
There are three common approaches you can take to determine if an object is present in an array. You can use:
- some
- find
- findIndex
Some Method
The some()
method returns true
if any one element in the array satisfies the condition in callback and is passed. Otherwise, it returns false
.
var lang = [ {name : "JavaScript"}, {name : "Java"}];
var hasJava = e => e.name === "Java";
var hasPython = e => e.name === "Python";
lang.some(hasJava);// true
lang.some(hasPython);// false
Find Method
The find()
method returns the value of the first element in the array that satisfies the condition in the callback. If the condition is not satisfied, then the function returns undefined.
var lang = [ {name : "JavaScript"}, {name : "Java"}];
var hasJava = e => e.name === "Java";
var hasPython = e => e.name === "Python";
lang.find(hasJava); // {name : "Java"}
lang.find(hasPython); // undefined
findIndex Method
The find()
method returns the index of the first element in the array that satisfies the condition in the callback. If the condition is not satisfied, then the function returns -1
.
var lang = [ {name : "JavaScript"}, {name : "Java"}];
var hasJava = e => e.name === "Java";
var hasPython = e => e.name === "Python";
lang.findIndex(hasJava); // 1
lang.findIndex(hasPython); // -1
You can also use normal for loop to check your conditions against the elements of the array.