How to Find Values in a JavaScript Array

Finding a value in an array is useful for effective data analysis. Learn how to discover what a JavaScript Array contains using indexOf, includes, for loop, some methods and more.  

Written by K. Jagathish
Published on May. 17, 2023
Image: Shutterstock / Built In
Image: Shutterstock / Built In
Brand Studio Logo

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

  1. IndexOf method
  2. Includes method
  3. For loop method
  4. Some method
  5. Find method
  6. 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:

  1. indexOf method
  2. Includes method
  3. 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];

More on JavaScript8 Common JavaScript Data Structures

 

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
A tutorial on the includes method in JavaScript. | Video: Code With Ania Kubów

 

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:

  1. some
  2. find
  3. 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

More on JavaScriptJavaScript PreventExtensions Method Explained

 

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.

Explore Job Matches.