How to check if an array contains a value matching some criterias in JavaScript

In this Article we will go through how to check if an array contains a value matching some criterias only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const contains = (arr, criteria) => arr.some(v => criteria(v));

#Or

const contains = (arr, criteria) => arr.some(criteria);

#Or

const contains = (arr,criteria) => arr.filter(criteria).length > 0;

#Example

contains([10, 20, 30], v => v > 25 );               // true
contains([10, 20, 30], v => v > 100 || v < 15 );    // true
contains([10, 20, 30], v => v > 100 );              // false