Definition of JavaScript every()
In Javascript, every() is a method that helps in checking whether elements in a given array satisfies a particular condition mentioned. If all the elements satisfy the condition, then true will be returned, else false will be returned. There are certain conditions to call this method. They are:
- An array should be present.
- Used in the case where each and every element in the array should be tested.
- To check whether all the elements fulfill a certain condition.
In the below sections, we will see the syntax, working and examples of Javascript every() method.
Syntax:
Below is the syntax of every() method.
array.every(callback(currentvalue ,ind, arr), thisArg)
Here, there are two parameters such as:
- Callback: Function which test the condition on each and every array element. In this function, certain arguments such as currentvalue, ind, arr are used where Currentvalue is the element that is currently getting processed, ind is the index of the present element and arr is the array where we use the every() method. Among this, currentvalue is a required argument and the other two are optional.
- thisArg argument: Argument which is optional in every() method. If this argument is getting used, this value within the callback function will refer the thisArgargument.
Return value of every() method is:
- True: Callback function returns a true value if every element in the array satisfies the condition.
- False: Callback function returns a false value if at least one element in the array do not satisfy the condition.
Callback function will stop checking the array if it finds any element that do not satisfy the condition.
How does every() Method Works in JavaScript?
- In certain cases, we have to check whether each and every element in the array satisfies a particular condition. Normally what will you do?
- Yes. We will use a for loop to iterate and check all the elements whether they satisfy that particular condition.
- For example, let us consider an array of numbers [3, 5, 9]. The task is to check whether the numbers are greater than 1.
The sample code will be as follows:
for (int i = 0; i < num.length; i++) {
if (numbers[i] < 1) {
res = false;
break;
}
}
What is Happening in this code?
The numbers are iterated and if any element is not satisfying the condition, the res will be set as false and loop gets terminated. Even though the code is simple and straight, it is verbose. As a solution for this, JavaScript offers the every() method that permits you to check each and every element of an array fulfills a condition in a shorter and clear way.So, how will be the code?? Let us see it in the next section.
Examples to Implement every() in JavaScript
Below are some of the simple javascript programs to understand every() method in a better manner.
Example #1
Java script program to check whether the input element is greater than the array elements.
Code:
<!DOCTYPE html>
<html>
<body>
<p>Input the number in the text field below to check whether the elements in the array are greater than it.</p>
<p>Number: <input type="number" id="NumToCheck" value="89"></p>
<p>Click the button...</p>
<button onclick="sample()">Try it</button>
<p>Are the numbers in the array are above the input number? <span id="demo"></span></p>
<script>
var nums = [45, 32, 78, 21];
function checkNum(num) {
return num >= document.getElementById("NumToCheck").value;
}
function sample() {
document.getElementById("demo").innerHTML = nums.every(checkNum);
}
</script>
</body>
</html>
Output:
On executing the code, a number will be asked to input in order to check whether it is greater than all the numbers in the array. Here, 89 is given as input and it can be clearly seen from the code that none of the numbers in the array are greater than 89. Therefore, on clicking the button, the function which contains the every() method will be called and false is returned.
In the next case, input number is given as 10 and as all the numbers are greater than 10, true is returned.
Example #2
Java script program to check whether the Gender values in the given array are the same.
Code:
<!DOCTYPE html>
<html>
<body>
<p>Click the below button to check whether the values of the Gender in the array are same.</p>
<button onclick="sample()">Try it</button>
<p id="demo"></p>
<script>
var emp = [
{ name: "Anna", Gender: "Female"},
{ name: "Iza", Gender: "Female"},
{ name: "Norah", Gender: "Female"},
{ name: "Adam", Gender: "Male"}
];
function isSameGender(elm,ind,arr) {
// since there is no element to compare to, there is no need to check the firt element
if (ind === 0){
return true;
}
else {
//compare each element with the previous element
return (elm.Gender === arr[ind - 1].Gender);
}
}
function sample() {
document.getElementById("demo").innerHTML = emp.every(isSameGender);
}
</script>
</body>
</html>
Output:
On executing the code, a button will be displayed similar to example 1. On clicking the button, the function which contains the every() method will be called. Here, every() method checks whether the value of Gender for all the names are the same. It can be clearly seen from the code that one of the names in the array is adam and gender is male. Therefore, on clicking the button, false is returned.
In the next case, the Name of adam is changed to annamu and Gender is changed to Female. Since all the values if Gender are the same, true is returned.
Example #3
Java script program to check whether the numbers in the given array are odd.
Code:
<!DOCTYPE html>
<html>
<body>
<script>
//function used to check negative numbers
function isodd(element, index, array)
{
return (element % 2 == 1);
}
var a1 = [7, 9, 13, 21, 73];
if(a1.every(isodd)==true)
document.write("All the numbers in the array a1 are odd numbers<br>");
else
document.write("All the numbers in the array a1 are not odd numbers<br>");
</script>
</body>
</html>
Output:
Here, all the numbers in the array are checked whether they are odd. As you can see, all the numbers are odd and the print statement corresponding to that condition is printed.