DEV Community

Randy Rivera
Randy Rivera

Posted on

Using the every Method in an array

  • The every method works with arrays to check if every element passes a particular test. It returns a Boolean value true if all values meet the criteria, false if not.

  • Example, the following code would check if every element in arr is positive.

function check(arr) {
return arr.every(function(num) {
  return num > 0;
})

}
console.log(check([1, 2, 3, -4, 5]));
Enter fullscreen mode Exit fullscreen mode
  • check([1, 2, 3, -4, 5]) should return false

Using the some Method

  • The some method works with arrays to check if any element passes a particular test. It returns a Boolean value true if any of the values meet the criteria, false if not.
  • Ex:
function check(arr) {
return arr.some(function(num) {
  return num > 0;
})

}
console.log(check([1, 2, 3, -4, 5]));
// would return true
Enter fullscreen mode Exit fullscreen mode

Top comments (0)