Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 9, 2021 09:26 pm GMT

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]));
  • 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

Original Link: https://dev.to/rthefounding/using-the-every-method-in-an-array-1j64

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To