Quantcast
Channel: How do I check if an array includes a value in JavaScript? - Stack Overflow
Viewing all articles
Browse latest Browse all 66

Answer by Ran Turner for How do I check if an array includes a value in JavaScript?

$
0
0

There are a couple of options for doing that including includes, some, find, findIndex.

const array = [1, 2, 3, 4, 5, 6, 7];console.log(array.includes(3));//includes() determines whether an array includes a certain value among its entriesconsole.log(array.some(x => x === 3)); //some() tests if at least one element in the array passes the test implemented by the provided functionconsole.log(array.find(x => x === 3) ? true : false);//find() returns the value of the first element in the provided array that satisfies the provided testing functionconsole.log(array.findIndex(x => x === 3) > -1 ? true : false);//findIndex() returns the index of the first element in the array that satisfies the provided testing function, else returning -1.

Viewing all articles
Browse latest Browse all 66

Trending Articles