6

Using the every Method in an array

 3 years ago
source link: https://dev.to/rthefounding/using-the-every-method-in-an-array-1j64
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Using the every Method in an array

Jul 9

・1 min read

  • 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 modeExit 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.
function check(arr) {
return arr.some(function(num) {
  return num > 0;
})

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

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK