Search code snippets, questions, articles...

Test every item of array against some condition in javascript

We can test every value of an array against some condition using Array.every() method and if all the items satisfy the condition, it will return true.
const scores = [86, 70, 56, 52, 78, 83];

const is_passed = scores.every( score => score > 33 );

// -> true
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

In the above code snippet, we have an array of scores that contains marks obtained by a student. If all marks are greater than 33 then the student is passed. It will return true if the student is passed. And false if the student is failed.

Was this helpful?