.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
isPositiveis defined using the syntaxdef isPositive(item).Inside the function,
item > 0is returned, which checks if each item is greater than 0.num.every(isPositive)is called, applying theisPositivefunction to each element, resulting intruesince all elements are greater than 0.
num = [1,2,3,4,5]
def isPositive(item) (
return item > 0
)
log num.every(isPositive)
// trueMore 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
))
// trueLambdas
The every method can also be used with lambda functions.
num = [1,2,3,4,5]
log num.every(item -> item > 0)
// trueLast updated
Was this helpful?