How to check if an element is present in an array in JavaScript ?

Last Updated on :February 26, 2024

In this article, we will look into different ways to find out if a javaScript array contains a value / element

1. Using Arrays.includes() 

The includes() method checks whether an array contains a specific value among its entries and returns true or false accordingly.


//calling Arrays.includes() on primitive array
let numArray = [10, 20, 30, 40, 50];

console.log(numArray.includes(40));  
// expected output: true

console.log(numArray.includes(60));  
// expected output: false


//calling Arrays.includes() on string array
let strArray = ["Test", "Devtech", "blog", "Website"];
<br>console.log(strArray.includes("Devtech"));
// expected output: true

console.log(strArray.includes("Hello"));
// expected output: false

2. Using Array.indexOf()

indexOf() method returns the index of the first occurrence at which a given element can be found in the array or -1 if element is not present in the array.

In the below example , we can check the first occurrence of a string present in a string array.

For the first case the element is present and it’s position is returned. 

In the second case the return value is -1 that indicates the element is not present.


let strArray = ["Dev", "Tech", "Info", "Website","Tech"];
console.log(strArray.indexOf("Tech")); // expected output: 1
console.log(strArray.indexOf("Blog")); // expected output: -1

3. Using For Loop

You can write your own function to perform check if the element is present in the array or not.

Please check below example


function containsInArray(element, elementArr) {
 var length = elementArr.length;
 for (var i = 0; i < length; i++) {
 if (elementArr[i] == element)
  return true;
 }
 return false;
}

let strArray = ["Dev", "Tech", "Info", "Website","Tech"];

if (containsInArray("Tech",strArray)) {
    console.log("Element is present");
} else {
    console.log("Element is not present");
}
// expected output: Element is present


How to check if an object is in array of objects

In the above example we have seen how to search an primitive/string value in an array.

This example we will see if an object already exists in an array in javascript using some() method.

some() method returns true if an element present in the array of objects otherwise it returns false. 

The some() method executes the callback function once for each element present in the array.It returns true if it finds an element for which the provided function returns true; otherwise it returns false. 


var personsArray = [
    { firstName: "Rakesh", lastName: "Panigrahi", age: 30 },
    { firstName: "Raj", lastName: "Kumar", age: 29 },
    { firstName: "Rabi", lastName: "Singh", age: 35 }
  ];

 var person = { firstName: "Raj", lastName: "Kumar", age: 29 };

 var result = personsArray.some(function (element) {
    return (element.firstName === person.firstName
      && element.lastName === person.lastName
      && element.age === person.age
    )
  });

 console.log(result);
// expected output: true

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *