What is the every() method in JavaScript?

The every() method checks if all the elements in an array pass a test; the test is passed to the method as a function. The method returns true if all the elements pass the test, and false if otherwise.

The function returns false immediately after encountering the first element that fails the condition.

Syntax

The syntax is shown​ below:

svg viewer

Code

function isPositive(element) {
return element >= 0;
}
let arr = [1, 4, 82, 45, 6]
console.log(arr.every(isPositive));
let arr = [11, 12, 13, 14, 15]
console.log(arr.every(element => element > 10));
function isCanine(element, index, arr) {
console.log("Checking if " + arr[index] + " is a canine.")
return (element == "dog" || element == "doggo")
}
let arr = ["dog", "cat", "doggo"]
console.log(arr.every(isCanine));
Copyright ©2024 Educative, Inc. All rights reserved