JavaScript Array every()

In this post, we will discuss every() method available in JavaScript Array.

every()

The every() method is used to check if the elements in the array are matched with the condition specified. If all the elements matched with the condition specified, true is returned, otherwise false is returned. We need to write a function for condition and pass the function name to the every() as a parameter.

Syntax:

array_name.every(function_name)

Here, 'array_name' is the name of the array. It will take 'function_name' as a parameter.

Example 1:-

Let us create an array that hold 10 subjects and check if all the elements are equal to "php" or not using every() function. So the condition we need to provide inside the function will be - subjects=="php".

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
function condition1(subjects) {
  return subjects=="php";
}
// Check if all the elements in the array are equal to "php" or not.
document.writeln("Does all elements in the array are equal to php? <br>");
document.writeln(subjects1.every(condition1));
</script>
</body>
</html>

Output:

Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Does all elements in the array are equal to php?
false

We can see that all elements in the array are not equal to "php". So false is returned.

Example 2:-

Let us create an array that hold 10 subjects and check if all the elements are of length-3. So the condition we need to provide inside the function will be - subjects.length==3.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","c++","sql","iot","jsp","jsp","php","php","sql","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
function condition1(subjects) {
  return subjects.length==3;
}
// Check if length of all the elements in the array are equal to 3 or not.
document.writeln("Does all elements in the array are of length-3 ? <br>");
document.writeln(subjects1.every(condition1));
</script>
</body>
</html>

Output:

Subjects:
php,c++,sql,iot,jsp,jsp,php,php,sql,php
Does all elements in the array are of length-3 ?
true

We can see that the length of all the elements in the array are equal to 3. So true is returned.

Example 3:-

Let us create an array that hold 10 integers and check if all the elements are greater than 40. So the condition we need to provide inside the function will be - marks>40.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 integers
const marks =[12,34,22,45,67,89,0,0,7,56];
document.writeln("Marks: <br>");
document.writeln(marks);
document.writeln("<br>");
document.writeln("<br>");
function condition1(marks) {
  return marks>40;
}
// Check if all the elements in the array are greater than 40
document.writeln("Does all elements in the array greater than 40 ? <br>");
document.writeln(marks.every(condition1));
</script>
</body>
</html>

Output:

Marks:
12,34,22,45,67,89,0,0,7,56
Does all elements in the array greater than 40 ?
false

We can see that all the elements are not greater than 40. So false is returned.

Example 4:-

Let us create an array that hold 10 integers and check if all the elements are greater than 10 and divisible by 2. So the condition we need to provide inside the function will be - marks>10 && marks%2==0.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 integers
const marks =[12,22,44,66,88,100,24,56,88,90];
document.writeln("Marks: <br>");
document.writeln(marks);
document.writeln("<br>");
document.writeln("<br>");
function condition1(marks) {
  return marks>10 && marks%2==0;
}
// Check if all the elements are greater than 10 and divisible by 2.
document.writeln("Does all elements in the array greater than 10 and divisible by 2 ? <br>");
document.writeln(marks.every(condition1));
</script>
</body>
</html>

Output:

Marks:
12,22,44,66,88,100,24,56,88,90
Does all elements in the array greater than 10 and divisible by 2 ?
true

We can see that all the elements are greater than 10 and divisible by 2. So true is returned.