How do I check if an array includes a value in JavaScript?

ID : 69

viewed : 675

Tags : javascriptarraysalgorithmtime-complexityjavascript-objectsjavascript

Top 5 Answer for How do I check if an array includes a value in JavaScript?

vote vote


Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE:

console.log(['joe', 'jane', 'mary'].includes('jane')); //true

You can also use Array#indexOf, which is less direct, but doesn't require polyfills for outdated browsers.

console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); //true

Many frameworks also offer similar methods:

Notice that some frameworks implement this as a function, while others add the function to the array prototype.

vote vote


Update from 2019: This answer is from 2008 (11 years old!) and is not relevant for modern JS usage. The promised performance improvement was based on a benchmark done in browsers of that time. It might not be relevant to modern JS execution contexts. If you need an easy solution, look for other answers. If you need the best performance, benchmark for yourself in the relevant execution environments.

As others have said, the iteration through the array is probably the best way, but it has been proven that a decreasing while loop is the fastest way to iterate in JavaScript. So you may want to rewrite your code as follows:

function contains(a, obj) {     var i = a.length;     while (i--) {        if (a[i] === obj) {            return true;        }     }     return false; } 

Of course, you may as well extend Array prototype:

Array.prototype.contains = function(obj) {     var i = this.length;     while (i--) {         if (this[i] === obj) {             return true;         }     }     return false; } 

And now you can simply use the following:

alert([1, 2, 3].contains(2)); // => true alert([1, 2, 3].contains('2')); // => false 
vote vote


indexOf maybe, but it's a "JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard."


[1, 2, 3].indexOf(1) => 0 ["foo", "bar", "baz"].indexOf("bar") => 1 [1, 2, 3].indexOf(4) => -1 

AFAICS Microsoft does not offer some kind of alternative to this, but you can add similar functionality to arrays in Internet Explorer (and other browsers that don't support indexOf) if you want to, as a quick Google search reveals (for example, this one).

vote vote


ECMAScript 7 introduces Array.prototype.includes.

It can be used like this:

[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false 

It also accepts an optional second argument fromIndex:

[1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true 

Unlike indexOf, which uses Strict Equality Comparison, includes compares using SameValueZero equality algorithm. That means that you can detect if an array includes a NaN:

[1, 2, NaN].includes(NaN); // true 

Also unlike indexOf, includes does not skip missing indices:

new Array(5).includes(undefined); // true 

Currently it's still a draft but can be polyfilled to make it work on all browsers.

vote vote


The top answers assume primitive types but if you want to find out if an array contains an object with some trait, Array.prototype.some() is an elegant solution:

const items = [ {a: '1'}, {a: '2'}, {a: '3'} ]  items.some(item => item.a === '3')  // returns true items.some(item => item.a === '4')  // returns false 

The nice thing about it is that the iteration is aborted once the element is found so unnecessary iteration cycles are spared.

Also, it fits nicely in an if statement since it returns a boolean:

if (items.some(item => item.a === '3')) {   // do something } 

* As jamess pointed out in the comment, at the time of this answer, September 2018, Array.prototype.some() is fully supported: support table

Top 3 video Explaining How do I check if an array includes a value in JavaScript?