Trusted answers to developer questions

What is the every() method in JavaScript?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

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));

RELATED TAGS

every
method
javascript
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?