The every method in JavaScript arrays evaluates if all the elements in a specified array meet a certain condition. It yields true if every element in the array fulfills the condition; otherwise, it returns false.
Syntax
The syntax for the every method is denoted as follows:
array.every(callback(currentvalue,index,arr),thisArg)
Parameter
callback - It denotes the function responsible for evaluating the condition.
currentvalue - The current element of array.
index - This parameter is not mandatory. It represents the index of the current element.
arr - This parameter is not mandatory. It refers to the array that the every method processes.
thisArg - This parameter is not mandatory. It specifies the value to be employed as this when the callback function is executed.
Return
A Boolean value.
JavaScript Array every method example
Let's see some examples of every method.
Example 1
Let's check the marks of a student.
<script>
var marks=[50,40,45,37,20];
function check(value)
{
return value>30; //return false, as marks[4]=20
}
document.writeln(marks.every(check));
</script>
Output:
Example 2
In this instance, we will evaluate if the count of elements within an array meets the defined criteria.
<script>
function test(element, index, array) {
return index < 4;
}
document.writeln([21,32,2,43].every(test)); //true
document.writeln([21,32,2,43,35].every(test)); //false
</script>
Output:
true false