Sitemap
Manikandan L

Manikandan L

MERN stack developer

Javascript Array.every() method

4 min readOct 5, 2023

--

Press enter or click to view image in full size

The every() executes a callback function for each element in the array of iteration.Every method accepts two arguments one is callback function and the another one is thisArg

Every does not mutate or modify the original array, but the function provided as callback function can able to modify the original array.

Note, however, that the length of the array is saved before the first iteration of callback function.

callback function will not visit any elements added beyond the array's initial length when the call to every() began.

Callback function accepts 3 arguments like current element, index, and the original array.Index and the original array are optional. Current element is the element of each iteration in the array.

thisArg This parameter is optional, it holds the context to be passed as this is to be used while executing the callback function. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.

Every method returns boolean values only that is true or false.Based on the user condition it returns the value

Once the callback function returns the false value the every method stops the iteration and returns the false value.

let arr = [1, 2, 3, 4, 5, 6, 7, 8];

let isValuesLessThan5 = arr.every((ele, i) => {
console.log('Iteration' + '_' + i, ele); return ele < 5;
});

console.log('isValuesLessThan5', isValuesLessThan5);

//output
Iteration_0 1
Iteration_1 2
Iteration_2 3
Iteration_3 4
Iteration_4 5
isValuesLessThan5 false

The above code stops the iteration when the callback functions returns false value, so the code stops at the 5th iteration, in the 5th the callback returns false.

let arr = [1, 2, 3, 4, 5, 6, 7, 8];

let isValuesLessThan9 = arr.every((ele, i) => {
console.log('iteration' + '_' + i, ele);
return ele < 9;
});

console.log('isValuesLessThan9', isValuesLessThan9);

//output
iteration_0 1
iteration_1 2
iteration_2 3
iteration_3 4
iteration_4 5
iteration_5 6
iteration_6 7
iteration_7 8

isValuesLessThan9 true
let arr = [1, 2, 3, 4, 5, 6, 7, 8];

function checkGreaterOrNot(ele, i) {
console.log('iteration' + '_' + i, ele);
return ele < 9;
}

let isValuesLessThan9 = arr.every(checkGreaterOrNot);

console.log('isValuesLessThan9', isValuesLessThan9);

//output
iteration_0 1
iteration_1 2
iteration_2 3
iteration_3 4
iteration_4 5
iteration_5 6
iteration_6 7
iteration_7 8

isValuesLessThan9 true

It does not specifically check for empty values by default. Instead, it checks whether the callback function returns true for every element in the array.

let arr = [3, 4, , 5, 6, 7, , , 8];

let checkValueGreaterThan2 = arr.every((item) => {
return item > 2;
});

console.log(checkValueGreaterThan2); // true

Evevry method in JavaScript will not run its predicate on empty slots (sparse arrays). It only checks the elements that have been explicitly set in the array and skips any empty or undefined slots.

Get Manikandan L’s stories in your inbox

Join Medium for free to get updates from this writer.

This is why the above code returns true.

Modifying the original Array elements

will check whether the conditions satisfied or not ,after modifying the array elements of each iteration

let arr = [1, 2, 3, 4];

let lessThan3 = arr.every((item, i) => {
if (i + 1 < arr.length) {
arr[i + 1] -= 2;
}
console.log('item', item);
return item <= 2;
});

console.log('lessThan3', lessThan3); //lessThan3 true

console.log(arr); //[ 1, 0, 1, 2 ]

// output console.log('item', item);

item 1
item 0
item 1
item 2

For each iteration we change the next index value to reduce 2, so that the reason the condition is satisfied

Delete the array elements

We can even delete elements from an array in the every loop:

let arr = [5, 12, 14, 55, 65];
let check = arr.every((el, index, array) => {
arr.pop();
return el < 15;
});
console.log(check) // true
console.log(arr);//[5,12]

In the above code array index 3 and 4 is not satisfied the condition , the callback function returns false only, but each iteration we removed the last item from the original array, so for the first and second iteration 55 and 65 removed from the array and third iteration 14 also removed , the callback condition satisfied and every return true.

Adding the element to the Array

let arr = [1, 2, 3, 4, 5];

let lessThan6 = arr.every((item) => {
arr.push(item + 5);
return item < 6;
});

console.log(lessThan6); //true
console.log(arr); //[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note, however, that the length of the array is saved before the first iteration of callback function.

callback function will not visit any elements added beyond the array’s initial length when the call to every() began.

The initial array elements less than 6 only, so the every methods return true, because it will not call the callback function beyond the array’s initial length.

--

--