JavaScript every() Method
The every()
method is used to test whether all elements in an array pass the test implemented by the provided function. It returns true
if the callback function returns a truthy value for every array element. Otherwise, it returns false
.
Syntax
The syntax for the every()
method is as follows:
array.every(function(currentValue, index, arr), thisValue)
currentValue
: The current element being processed in the array.index
(Optional): The index of the current element being processed in the array.arr
(Optional): The array thatevery()
is being applied upon.thisValue
(Optional): A value to use asthis
while executing the callback function.
Example
Here's an example that uses the every()
method to check if all values in an array are even:
const numbers = [2, 4, 6, 8];
const allEven = numbers.every(function(num) {
return num % 2 === 0;
});
console.log(allEven); // true
Output
The code above will output true
because all values in the numbers
array are even.