.every(callback)

Description

The every method tests whether all elements in the array pass the test implemented by the provided callback function. It returns a Boolean value.

Parameters

The every method requires the array to test and the custom function that defines the test for each element.

Usage On Arrays

In the provided examples, the every method is used to check if all elements in the array num, which contains the numbers [1,2,3,4,5], are greater than 0.

  • A separate function isPositive is defined using the syntax def isPositive(item).

  • Inside the function, item > 0 is returned, which checks if each item is greater than 0.

  • num.every(isPositive) is called, applying the isPositive function to each element, resulting in true since all elements are greater than 0.

num = [1,2,3,4,5]

def isPositive(item) (
  return item > 0
)

log num.every(isPositive)
// true

More Examples

The following example demonstrates how to use the every method with an inline function.

num = [1,2,3,4,5]

log num.every(def(item) -> (
  return item > 0
))
// true

Lambdas

The every method can also be used with lambda functions.

num = [1,2,3,4,5]

log num.every(item -> (item > 0))
// true

Last updated

Was this helpful?