Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

every
method
javascript

What is the every() method in JavaScript?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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));
The every method is used here to check if all the elements in the array satisfy the condition in the isPositive function.
let arr = [11, 12, 13, 14, 15]
console.log(arr.every(element => element > 10));
It can also be used with lambda functions. Here, every element is checked if it is greater than 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));
In this example, note how the function returns after encountering the first value that fails the condition(cat is not a canine). The rest of the array is not examined.

RELATED TAGS

every
method
javascript
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring