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 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.