The every()
method in JavaScript is used to check if all elements in an array pass a specified test. This method returns a Boolean value indicating whether all elements in the array pass the test.
Here is an example of how the every()
method works:
const array1 = [1, 2, 3, 4, 5];
const result = array1.every(element => element > 0);
// the result will be: true
In the code example above, we first declare an array called array1
that contains the numbers 1
through 5
. We then use the every()
method to check if all elements in the array are greater than 0
. The every()
method takes a callback function as an argument, and this callback function is called for each element in the array. The callback function should return a Boolean value indicating whether the element passes the test. In this case, the callback function simply checks if the element is greater than 0
.
As a result of calling the every()
method, the result
variable will be set to true
, because all elements in the array1
array are greater than 0
. If any element in the array were not greater than 0
, the result
variable would be set to false
.
Leave a Reply