# How to check if all values in an array meet a condition in JavaScript

Published on

You can use the Array.prototype.every() method to determine if all the elements in an array meet a condition. The method accepts a function that will be used to check each element. In the below example the isGreaterThanFive function is used to determine if a score qualifies.

const redScores = [10, 7, 9];
const blueScores = [4, 7, 9];

const isGreaterThanFive = (n) => n > 5;

const allRedScoresQualify = redScores.every(isGreaterThanFive);
console.log(allRedScoresQualify);
// true

const allBlueScoresQualify = blueScores.every(isGreaterThanFive);
console.log(allBlueScoresQualify);
// false


If the test function is short enough then it can just be written in line as seen below. The trade off here is that you get less lines of code but lose the context of a named function.

const redScores = [10, 7, 9];

const allRedScoresQualify = redScores.every((n) => n > 5);
console.log(allRedScoresQualify);
// true