.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 syntaxdef isPositive(item)
.Inside the function,
item > 0
is returned, which checks if each item is greater than 0.num.every(isPositive)
is called, applying theisPositive
function to each element, resulting intrue
since all elements are greater than 0.
More Examples
The following example demonstrates how to use the every
method with an inline function.
Lambdas
The every
method can also be used with lambda functions.
Last updated
Was this helpful?