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 Med Aziz CHETOUI for How do I check if an array includes a value in JavaScript?

$
0
0

The best default method to check if value exist in array JavaScript is some()

Array.prototype.some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

const array = [1, 2, 3, 4, 5];// checks whether an element is evenconst even = (element) => element % 2 === 0;console.log(array.some(even));// expected output: true

The some method is the best one in Browser compatibilityBrowser compatibility

For more documentation Array.prototype.some() - JavaScript | MDN

Also you can use other two method is find() and includes(). with those method you can get your result but not the best one.

Array.prototype.find() - JavaScript | MDN

Array.prototype.includes() - JavaScript | MDN


Viewing all articles
Browse latest Browse all 66

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>