arrays support some and every

It turns out that arrays support some and every. Both functions take a predicate and they test if the predicate is true for some or every element of the array, respectively. So there's no need to combine filter with length to achieve the same. More on arrays functions in the Javascript docs.

const isEven = (f: number) => f % 2 === 0

// test if an array contains an even number
console.log([1, 2, 3].some(isEven)) // prints true
console.log([1, 3, 5].some(isEven)) // prints false

// test if an array consists only of even numbers
console.log([1, 2, 3].every(isEven)) // prints false
console.log([2, 4, 6].every(isEven)) // prints true

Would you like to connect? Subscribe via email or RSS , or follow me on Twitter!