Different ways to find a value present in an Array in JavaScript

Learn to check if the primitive value or object present in an array in JavaScript

Photo by Deva Darshan on Unsplash

Checking if a primitive value present in an array

You can use

  • indexOf
  • includes
  • Using normal For loop

method to check if a primitive value present in an array, Consider we have an array

var num = [1,2,3,4,5];

indexOf

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); // 2num.indexOf(6); //-1num.indexOf('3'); // -1

The NaN value is not handled in indexOf method.

var array = [NaN];array.indexOf(NaN); // -1

If your array can contain NaN , then you can use includes method

includes

The includes() method returns true if the value passed is present in the array, otherwise returns false.

num.includes(1); // truenum.includes(0); // false

Checking NaN values

var array = [NaN];array.includes(NaN); // true

Using For loop

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
}

Searching NaN, undefined, 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

Checking if an object present in an array

For this, you can use

  • some
  • find
  • findIndex

some

The some() method returns true if any one element in the array satisfies the condition in callback passed, otherwise returns false.

var lang =  [ {name : "JavaScript"}, {name : "Java"}];var hasJava =  e => e.name === "Java";var hasPython = e => e.name === "Python"; lang.some(hasJava);// truelang.some(hasPython);// false

find

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

The find() method returns the index of the first element in array that satisfies the condition in 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); // 1lang.findIndex(hasPython); // -1

You can also use normal for loop to check your conditions against the elements of the array.

Articles related to JavaScript.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store