Functions - JavaScript every()
Posted by Superadmin on May 07 2023 07:37:42

JavaScript every()

By Princy VictorPrincy Victor
  

JavaScript every()

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:

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:

Return value of every() method is:

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?

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:

JavaScript every()-1.1

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.

JavaScript every()-1.2

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:

Output-1.3

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.

Output-1.4

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:

Output-1.5

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.