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

includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

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

var array = [50, 100, 200];

console.log(array.includes(50));
// expected output: true

console.log(array.includes(10));
// expected output: false

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

var array = [50, 100, 200];

console.log(array.indexOf(50) >= 0);
// expected output: true

console.log(array.indexOf(10) >= 0);
// expected output: false

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments