The
everymethod works with arrays to check if every element passes a particular test. It returns a Boolean valuetrueif 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]));
-
check([1, 2, 3, -4, 5])should returnfalse
Using the some Method
- The
somemethod works with arrays to check if any element passes a particular test. It returns a Boolean valuetrueif 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
Top comments (0)