JavaScript Array every() Method

By Arvind Rai, November 07, 2019
JavaScript Array every() method is used to test a given function for all its elements. The every() method returns boolean value. If every() method returns true, the test for given function is considered to be passed otherwise fail.
The function that needs to be tested should also return boolean values. The every() method will run the function for array elements one-by-one until it gets false return value by function for any element. The function test is considered to be passed only if the every() method runs for all array elements with function return value true and in this case every() method returns true value. The every() method does not change array. If the array is empty, then the provided function will not execute and in this case the every() method will return true.

Syntax:
Find the syntax of JavaScript Array every() method.
array.every(callback(element, index, array), thisArg) 
A. Parameters:
1. The callback is a function to test for each element of the array. It takes three arguments i.e. element, index and array.
a. The element is the current element being processed in the array.
b. The index is the index of the current element being processed in the array. It is optional.
c. The array is the array on which every() method is called on. It is optional.
2. The thisArg is a value to use as this when executing callback. It is optional.

B. Return value: The every() method returns boolean value. It returns true if the callback function returns true for every array element, otherwise, it returns false.

Supported browser:
JavaScript Array every() method is specified in ECMAScript 5.1 and implemented in JavaScript 1.6. The every() method is supported by Chrome, Edge (12), Firefox (1.5), Internet Explorer (9), Opera, Safari etc.

Using every() Method

Example-1: Find the example to check if array has all even numbers or all odd numbers.
function oddNumber(num) {
  return num % 2 == 1;
}

function evenNumber(num) {
  return num % 2 == 0;
}

var array = [2, 4, 6, 8, 10];
var result = array.every(evenNumber);
console.log(result); // true

result = array.every(oddNumber);
console.log(result); // false 
Example-2: Here we have an array of persons. We will check if all persons in array are female. We will also check if all persons are eligible to vote in election.
function isFemale(person) {
  return person.gender == 'female'
}

function canVote(person) {
  return person.age >= 18;
}

var persons = [
    {name: 'Seeta', age: 19, gender: 'female'},
    {name: 'Gauri', age: 20, gender: 'female'},
    {name: 'Radha', age: 16, gender: 'female'},
    {name: 'Durga', age: 22, gender: 'female'}            
];

//Are all persons in array female?
var result = persons.every(isFemale);
console.log(result); // true

//Can all persons in array vote in election?
result = persons.every(canVote);
console.log(result); // false 
Example-3: As we know that every() method can accept three arguments that are current element, index and array, out of which index and array are optional. Find the example to pass all these three arguments.
function isPassed(currentStudent, index, array) {
	console.log('--- Student id: ' + currentStudent.id + ' ---');
	console.log("Array Index: " + index);
	console.log("Array length: " + array.length);
	var passed = false;
	if (currentStudent.subjMarkA >= 40) {
	  passed = true;
	} else if (currentStudent.subjMarkB >= 50) {
	  passed = true;
	}
	console.log(' Is student passed: ' + passed);
	return passed;
}
var students = [
	{ id: 101, subjMarkA: 30, subjMarkB: 55 },
	{ id: 102, subjMarkA: 50, subjMarkB: 35 },
	{ id: 103, subjMarkA: 40, subjMarkB: 30 },
	{ id: 104, subjMarkA: 25, subjMarkB: 60 }
];

var result = students.every(isPassed);
console.log(result); // true 
Find the print screen of output in browser console.
JavaScript Array every() Method

Using Arrow Functions with every() Method

Example-1: We can use arrow functions with every() method.
//Are all array numbers greater than 20
var nums = [22, 30, 21, 25];    
var result = nums.every(num => num > 20);
console.log(result); // true

//Are all ages in array greater than or equal to 18
var ages = [18, 17, 20, 17];    
result = ages.every(data => data >= 18);
console.log(result); // false

//Are all array numbers even
var array1 = [2, 5, 6, 8];
result = array1.every(data => data % 2 == 0);
console.log(result); // false

//Are all array numbers odd
var array2 = [1, 3, 7, 11];
result = array2.every(data => data % 2 == 1);
console.log(result); // true 
Example-2: Here we have array of employees.
var employees = [
    {id: 101, profile: 'Team Lead', skill: 'Java'},
    {id: 102, profile: 'Developer', skill: 'Java'},
    {id: 103, profile: 'Team Lead', skill: 'Java'}
 ];    

//Are all employees team lead 
var result = employees.every(emp => emp.profile == 'Team Lead');
console.log(result); // false

//Have all employees Java skill
result = employees.every(emp => emp.skill == 'Java');
console.log(result); // true 
Example-3: Find the example of every() method with its all three arguments.
var names = ['Mahesh', 'Suresh', 'Krishna'];

//Have all names length in array > 5
var result = names.every((name, index, array) => {
     console.log('--- ' + name + ' ---');
     console.log('Index: ' + index);
     console.log('Array length: ' + array.length);
     console.log('Name length: ' + name.length);

     return name.length > 5;
});
console.log(result); // true 
Output
--- Mahesh ---
Index: 0
Array length: 3
Name length: 6
--- Suresh ---
Index: 1
Array length: 3
Name length: 6
--- Krishna ---
Index: 2
Array length: 3
Name length: 7 
true 

Testing every() Method with Adding, Modifying and Deleting Elements in Initial Array

Example-1: Adding elements:
We have an array of numbers and we will check if all numbers are even. In our function we will add more numbers to initial array and will see the impact on every() method.
var numArray = [2, 4, 6];
var result = numArray.every((num, index, array) => {
     array.push(7);
     console.log(array);     
     return num % 2 == 0;
});
console.log(result); // true 
Output
[ 2, 4, 6, 7 ]
[ 2, 4, 6, 7, 7 ]
[ 2, 4, 6, 7, 7, 7 ]
true 
We can see that adding elements to initial array by function is not impacting to every() method. Once the every() method starts, it iterates only maximum to initial array size.

Example-2: Modifying elements:
In this example, function will modify the initial array and we will see the impact on every() method.
var numArray = [2, 3, 5, 7];
var result = numArray.every((num, index, array) => {
    if (index + 1 < array.length) {
      array[index + 1] += 1;
      console.log(array);        
    }
    return num % 2 == 0;
});
console.log(result); // true 
Output
[ 2, 4, 5, 7 ]
[ 2, 4, 6, 7 ]
[ 2, 4, 6, 8 ]
true 
We can see that every() method is processing modified element of initial array.

Example-3: Deleting elements:
In this example, function will delete the elements from initial array and we will see the impact on every() method.
var numArray = [2, 4, 5, 7];
var result = numArray.every((num, index, array) => {
    array.pop();
    console.log(array);        
    return num % 2 == 0;
});
console.log(result); // true 
Output
[ 2, 4, 5 ]
[ 2, 4 ]
true 
We can see that every() method is not processing deleted elements from initial array.

Reference

Array.prototype.entries()
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us