I’ve already written a post in the past discussing arrays in JavaScript that explains the basic usage of JavaScript arrays. In this post, we’ll specifically talk about how to check if an array contains a specific value or not.
Both JavaScript and jQuery come with built-in methods that return the position of a value in an array. Let’s explore the most common approaches.
Using JavaScript Loop to Find an Item in an Array
An old-fashioned way to find a value in a specific array is to use a for loop. This is the most straightforward approach when you want to perform iteration over an array. Take a look at the following example:
let moviesList = ['The Godfather','Forrest Gump','The Matrix','Inception','Catch Me If You Can','Pulp Fiction'];
function findValueInArray(value, arr){
let result = "Doesn't exist";
for(let i=0; i<arr.length; i++){
let name = arr[i];
if(name == value){
result = 'Exist';
break;
}
}
return result;
}
findValueInArray('Pulp Fiction', moviesList);
// Result : Exist
findValueInArray('Avenger', moviesList);
// Result : Doesn't exist
As mentioned, this is an old-fashioned way to find an item in an array. Now let’s see how to do it more easily using the built-in methods that come with JavaScript and jQuery.
Using Array.indexOf()
The method Array.indexOf() in JavaScript helps us find an item in an array. If the item is found in the array, the method will return the index position of the item, and if the item does not exist, it will return ‘-1‘.
This method works for both arrays and strings.
Syntax
array-or-string.indexOf()Here’s an example in action:
let moviesList = ['The Godfather','Forrest Gump','The Matrix','Inception','Catch Me If You Can','Pulp Fiction'];
let string = "Catch Me If You Can";
// Find in Array
moviesList.indexOf('Pulp Fiction');
// Result: 5
moviesList.indexOf('The Nun');
// Result: -1
// Find in String
string.indexOf('M');
// Result: 6Note that indexOf() uses strict comparison and cannot detect NaN values. If you need to check for NaN in an array, use includes() instead.
Using jQuery.inArray()
The method jQuery.inArray() works in a similar fashion. It will return the index position if it finds the value we’re looking for, and alternatively, it will return ‘-1‘ if the value is not found in the array.
This method also works for both arrays and strings.
Syntax
jQuery.inArray(value, array [, fromIndex ])Here’s an example in action:
<html lang="en">
<head>
<!-- Include jQuery Here to test... -->
</head>
<body>
<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<div>"Pete" is in the array, but not at or after index 2, so <span></span></div>
<script>
let arr = [4, "Pete", 8, "John"];
let $spans = $("span");
$spans.eq(0).text(jQuery.inArray("John", arr));
$spans.eq(1).text(jQuery.inArray(4, arr));
$spans.eq(2).text(jQuery.inArray("Karl", arr));
$spans.eq(3).text(jQuery.inArray("Pete", arr, 2));
</script>
</body>
</html>When this code runs, the output will be as follows:
// Result: "John" found at 3
// Result: 4 found at 0
// Result: "Karl" not found, so -1
// Result: "Pete" is in the array, but not at or after index 2, so -1Using the includes() Method
An additional and simpler method to check if a value exists in an array is using the Array.includes() method. The includes() method determines whether an array includes a certain value among its entries, returning true or false.
This method works for both arrays and strings. Unlike
indexOf(), it correctly detectsNaNvalues.
Syntax
array.includes(valueToFind[, fromIndex])Here’s a working example:
const array = [1, 2, 3, 4, 5];
const value = 3;
if (array.includes(value)) {
console.log(`${value} exists in the array.`);
} else {
console.log(`${value} does not exist in the array.`);
}In this example, array.includes(value) will return true if the value 3 exists in the array, otherwise it will return false. You can replace value with any value you want to check for existence in the array.
Using Array.find() and Array.some()
When you need more control over the matching logic, such as searching within an array of objects, Array.find() and Array.some() are the right tools.
Array.find() returns the first element that satisfies the provided testing function, or undefined if no match is found:
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
const user = users.find(u => u.name === 'Bob');
console.log(user);
// Result: { name: 'Bob', age: 30 }Array.some() returns a boolean – true if at least one element passes the test, false otherwise. Think of it as includes() but with a callback function:
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
const hasAdult = users.some(u => u.age >= 30);
console.log(hasAdult);
// Result: trueWhich Method Should You Use?
Here’s a quick breakdown of when to use each method:
includes()– Best for simple existence checks. Returns a boolean, handlesNaN, and is the most readable option.indexOf()– Use when you need the position (index) of the value in the array, not just whether it exists.find()– Use when searching arrays of objects or when you need the matching element itself.some()– Use when you need a boolean result but with custom matching logic (like searching by object property).jQuery.inArray()– Use only if you’re already using jQuery in your project. For new projects, prefer native JavaScript methods.
FAQs
Common questions about checking values in JavaScript arrays:
indexOf() and includes() in JavaScript?
indexOf() returns the index position of the value (or -1 if not found), while includes() returns a boolean (true or false). Another key difference is that includes() can detect NaN values, while indexOf() cannot.Array.find() to get the matching object, or Array.some() to get a boolean result. For example: users.find(u => u.name === 'Bob') returns the object if found, or undefined if not. These methods accept a callback function, so you can match by any property.jQuery.inArray() still relevant?
jQuery.inArray() still works and is fine to use in existing jQuery projects. However, for new projects, native JavaScript methods like includes(), indexOf(), find(), and some() are preferred since they don't require an external library and are supported in all modern browsers.includes() check for objects in an array?
includes() uses strict equality (===), which checks reference identity for objects. Two objects with the same properties are not considered equal unless they are the exact same reference. For object searches, use Array.find() or Array.some() with a callback that compares the properties you need.Set first: const mySet = new Set(myArray). Then use mySet.has(value) for O(1) lookups instead of O(n). For one-time checks on smaller arrays, includes() is perfectly fast and readable.Summary
JavaScript provides several built-in methods for checking if a value exists in an array. For simple checks, includes() is the most readable and recommended approach. Use indexOf() when you need the position, and find() or some() when working with objects or complex conditions. If you’re in a jQuery project, jQuery.inArray() remains a valid option.
For more on working with arrays, take a look at the post on sorting JavaScript arrays and how to remove duplicate values from an array.


I have a question, if I have an array of objects, say an object with a first name and ID number, how do I search this array for the object with a specific ID number?
Hey. Let’s say you have an array like this:
And suppose you want to find the object with a specific ID (for example ‘987654321’), you can do it using find:
And if there is no such object, find will return undefined: