The quickest way to check if a value exists in a JavaScript array is includes(). It returns true or false and works in every modern browser:
const fruits = ['apple', 'banana', 'mango'];
fruits.includes('banana'); // true
fruits.includes('grape'); // false
That covers most cases. This post covers the ones it doesn’t: when you need the position of the value, when the array holds objects, when you check many values at once, and when you work in an older jQuery codebase.
If you’re new to arrays, start with this guide to JavaScript arrays first.
Using the includes() Method
The Array.includes() method checks whether an array contains a value and returns a boolean. It’s the most readable option for a simple yes-or-no check.
Syntax
array.includes(valueToFind[, fromIndex])
The optional fromIndex tells it where to start searching. Here’s a working example:
const numbers = [1, 2, 3, 4, 5];
const value = 3;
if (numbers.includes(value)) {
console.log(`${value} exists in the array.`);
} else {
console.log(`${value} does not exist in the array.`);
}
// Result: 3 exists in the array.
includes() compares values strictly, so '3' and 3 are not a match. Unlike indexOf(), it does find NaN.
Strings have their own
includes()method too:'Catch Me If You Can'.includes('Me')returnstrue. On strings it matches any substring, not only whole values.
Using indexOf() to Get the Position
The Array.indexOf() method returns the index of the first match, or -1 if the value isn’t in the array. Use it when you need to know where the value is, not only whether it’s there.
Syntax
array.indexOf(searchElement[, fromIndex])
Here’s an example in action:
const moviesList = ['The Godfather', 'Forrest Gump', 'The Matrix', 'Inception', 'Catch Me If You Can', 'Pulp Fiction'];
moviesList.indexOf('Pulp Fiction');
// Result: 5
moviesList.indexOf('The Nun');
// Result: -1
// As an existence check, compare against -1
if (moviesList.indexOf('Inception') !== -1) {
console.log('Found it');
}
Never write if (arr.indexOf(value)): a match at index 0 is falsy and a miss (-1) is truthy, so the check works backwards. Compare against -1, or use includes().
Like includes(), indexOf() uses strict comparison, but it can’t detect NaN: [NaN].indexOf(NaN) returns -1.
For more on positions, including findIndex() and lastIndexOf(), see how to find the index of a value in an array.
Checking an Array of Objects with some() and find()
includes() and indexOf() don’t help with objects. They compare objects by reference, so two objects with the same properties never match unless they’re the exact same object. (If JavaScript objects are new to you, start there.)
Instead, pass a callback that tests the property you care about. Array.some() returns true if at least one element passes the test:
const users = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 }
];
const hasBob = users.some(u => u.name === 'Bob');
console.log(hasBob);
// Result: true
Array.find() returns the first matching element itself, or undefined if nothing matches:
const user = users.find(u => u.name === 'Bob');
console.log(user);
// Result: { name: 'Bob', age: 30 }
Use some() when you only need a yes or no, and find() when you need to read or change the matching object. To check whether two whole arrays hold the same values, see comparing arrays in JavaScript.
Case-Insensitive and Multiple-Value Checks
includes() is case-sensitive, so 'Apple' won’t match 'apple'. For a case-insensitive check, use some() and lowercase both sides:
const tags = ['JavaScript', 'CSS', 'WordPress'];
const search = 'css';
tags.some(tag => tag.toLowerCase() === search.toLowerCase());
// Result: true
To check several values at once, combine includes() with every() (all of them must exist) or some() (at least one must exist):
const skills = ['html', 'css', 'javascript'];
['html', 'css'].every(s => skills.includes(s));
// Result: true (all exist)
['php', 'css'].some(s => skills.includes(s));
// Result: true (at least one exists)
Fast Lookups in Large Arrays with Set
Every call to includes() scans the array from the start. That’s fine for one check, but if you test many values against the same large array, the scans add up.
A Set solves this. Build it once, then use has() for each check:
const allowedIds = new Set([101, 205, 309, 412 /* ...thousands more */]);
allowedIds.has(309); // true
allowedIds.has(999); // false
Engines store a Set in a hash table, so has() doesn’t walk through every item and stays fast as the collection grows. Building the Set costs one pass over the array, so it only pays off when you run many lookups. A Set also drops duplicates, which is why it’s the shortest way to remove duplicate values from an array.
Using a for Loop
Before these methods existed, the way to search an array was a plain for loop. You rarely need it now, but it still works everywhere and lets you stop as soon as you find a match:
function contains(arr, value) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
return true;
}
}
return false;
}
contains(['The Matrix', 'Inception'], 'Inception'); // true
contains(['The Matrix', 'Inception'], 'Avatar'); // false
includes() does the same thing in one line, so prefer it unless the loop body needs to do more than compare.
Using jQuery.inArray()
If your project already uses jQuery, jQuery.inArray() is still available in jQuery 4.0 (released January 2026). Internally it just calls the native indexOf(), so it returns the index of the value or -1, never a boolean.
Syntax
jQuery.inArray(value, array[, fromIndex])
Here’s an example in action:
const arr = [4, 'Pete', 8, 'John'];
$.inArray('John', arr); // 3
$.inArray(4, arr); // 0
$.inArray('Karl', arr); // -1
$.inArray('Pete', arr, 2); // -1 (the search starts at index 2)
The same trap as with indexOf() applies here, and it’s the most common inArray() bug:
// Wrong: 'John' at index 0 is falsy, a miss (-1) is truthy
if ($.inArray('John', arr)) { ... }
// Right
if ($.inArray('John', arr) !== -1) { ... }
Despite what some tutorials say,
inArray()is not a string search. Given a string, it only matches single characters:$.inArray('Me', 'Catch Me')returns-1. Use the stringincludes()method for text.
For an array of objects in jQuery, use $.grep(), which returns the matching elements:
const hasBob = $.grep(users, u => u.name === 'Bob').length > 0;
// Result: true
In new code there’s no reason to load jQuery for this. The native methods above do the same job with no library.
Which Method Should You Use?
Here’s a quick comparison:
| Method | Returns | Use it when |
|---|---|---|
includes() |
true / false |
You need a simple check for a string, number or other primitive value |
indexOf() |
Index or -1 |
You need the position of the value |
some() |
true / false |
The array holds objects, or the match needs custom logic |
find() |
Element or undefined |
You need the matching element itself |
Set.has() |
true / false |
You run many lookups against the same large list |
$.inArray() |
Index or -1 |
You’re maintaining existing jQuery code |
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 difference is that includes() can detect NaN values, while indexOf() cannot.
Array.some() for a boolean result, or Array.find() to get the matching object. For example, users.some(u => u.name === 'Bob') returns true if any user is named Bob. In jQuery, $.grep(users, u => u.name === 'Bob').length > 0 does the same.
jQuery.inArray() still relevant?
indexOf() internally, so for new code use includes(), indexOf(), some() or find() directly. They need no library and work in all modern browsers.
includes() check for objects in an array?
includes() compares objects by reference, so two objects with the same properties are not equal. To search by property, use some() or find() with a callback.
includes() is case-sensitive. Use some() and lowercase both sides: arr.some(item => item.toLowerCase() === value.toLowerCase()).
every() with includes(): ['a', 'b'].every(v => arr.includes(v)) returns true only if both values exist. Swap every() for some() to check that at least one of them exists.
Set once with new Set(myArray) and use mySet.has(value). Lookups in a Set don't scan every item. For a single check, or a small array, includes() is fast enough and easier to read.
Summary
For a simple check, use includes(). Switch to indexOf() when you need the position, to some() or find() when the array holds objects, and to a Set when you run many lookups against the same data. jQuery.inArray() still works in jQuery projects, as long as you compare its result against -1.
In my own code I reach for includes() first and switch to some() as soon as the array holds objects. For more array work, see the post on sorting JavaScript arrays.
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: