Open In App

What is the use of the every() method in JavaScript Arrays ?

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.




let numbers = [2, 4, 6, 8, 10];
 
// Check if all numbers are even
let allEven = numbers.every(function(num) {
  return num % 2 === 0;
});
 
console.log(allEven); // Output: true

Output
true

Article Tags :