What is the use of the every() method in JavaScript Arrays ?
Last Updated :
31 Jan, 2024
The every()
method in JavaScript is used to check whether all elements in an array satisfy a particular condition. It returns a boolean value – true
if all elements meet the specified condition, and false
otherwise.
Example: Here, the every()
method is used to check if every element in the numbers
array is even. Since all elements in this case are even, the every()
method returns true
.
Javascript
let numbers = [2, 4, 6, 8, 10];
let allEven = numbers.every( function (num) {
return num % 2 === 0;
});
console.log(allEven);
|
Share your thoughts in the comments
Please Login to comment...