Arrays in JavaScript are used to store multiple data. These can be names, addresses, product names, numeric sequences, etc. There are many methods in JavaScript for Array objects that simplify common actions. With their help, long code parts can be replaced by methods that are only one line long.
For example, a simple check of array values, most likely, will require an algorithm based on a loop or function. Or it can be done with the standard method for arrays, called every()
, which checks an array against a certain condition. The value which it returns, depends on the result of all array elements checked.
How it works
The every()
method checks each array element against the given condition and returns a Boolean value. If all checked elements passed the test, then true is returned, if not - false.
Important to note that false is returned even if some or only one of the elements in the array fails the test. At the same time, during execution, this method doesn’t change the array and its elements in any way.
Like almost all array methods, every()
reads arrays from left to right, respectively from element 0 to the last declared element. However, it tests elements with assigned values only, so empty (and deleted) ones are ignored. Also note that method returns true if the array is empty (without elements).
The execution speed of this method depends on the complexity of the algorithm and tested array length. The array elements added after initialization will be ignored, while it takes into account values existing at the moment when it “reads” the element. Therefore, it's better to avoid array length and elements modify if every() doesn’t finish execution.
Syntax
Array which we want to check must be declared before, for correct execution. Also, you must specify a function that will perform (or refer to) the checking algorithm. So, the minimal syntax of the method is:
<Array>.every(callback(currentValue[, index[, array]])[, thisArg]);
The functionRule must be specified in the brackets of the every()
method, so it is a parameter - variable that affects the method execution. Parameters which are required for the method are called mandatory, while the rest are optional. In total, arrayName can take 5 parameters:
callback
- required parameter that refers to (or contain) the function where the verification algorithm will be implemented.
arrayElement
- (optional) the element of the array which is currently being checked.
elementIndex
- (optional) the index of the element which is currently being checked.
arrayName
- array for which every()
is being executed.
thisArgument
- value to call this.
Also, the functionRule can be realized in three ways: callback function, arrow function and inline callback function. It doesn't directly change method execution, but it affects the ability to use additional parameters. In particular, thisArgument cannot be used in arrow functions.
Syntax for
every()
with callback function:
let numbers = [75, 8, 90, 16, 93];
function CheckFunc(number) {
return number > 10;
}
console.log(numbers.every(CheckFunc)); // false
The method returns false when checking the numbers array, because the second element (8) doesn’t match the condition specified in the CheckFunc function. Keep in mind that the result will be the same regardless of type of used function.
Syntax for
every()
with arrow function:
let numbers = [75, 8, 90, 16, 93];
console.log(numbers.every((number) => number >10));
Syntax for
every()
with inline callback function:
let numbers = [75, 8, 90, 16, 93];
console.log(numbers.every(function(number){return number < 10}));
Result:
false
Test yourself
Test 1. Using
every()
and callback functions, check if arrays contain numbers below 15. Print the results of each check to the console.
Arrays:
numbers1: 14, 13, 11, 9, 1, 5, 2, 6, 8, 12
numbers2: 15, 16, 14, 1, 11, 5, 7, 4, 0, 8
numbers3: 9, 8, 8, 6, 5, 5, 5, 6, 1, 3
numbers4: 15, 15, 15, 15, 15, 15, 15, 15, 15, 14
Solution
From the start, we declare our numeric arrays with let. Then, we need to apply the method to each of them, and place it all inside the console.log()
,to get the results.
Then we create the LessThan
function, where an algorithm is implemented to check the numbers less than 15. And on the last step we place references to it in brackets of all calls of every()
.
let numbers1 = [14, 13, 11, 9, 1, 5, 2, 6, 8, 12];
let numbers2 = [15, 16, 14, 1, 11, 5, 7, 4, 0 ,8];
let numbers3 = [9, 8, 8, 6, 5, 5, 5, 6, 1, 3];
let numbers4 = [15, 15, 15, 15, 15, 15, 15, 15, 15, 14];
console.log(numbers1.every(LessThan));
console.log(numbers2.every(LessThan));
console.log(numbers3.every(LessThan));
console.log(numbers4.every(LessThan));
function LessThan(number){
return number < 15;
}
Result:
true
false
true
false
Test 2. Use the arrow function and
every()
method to check the sequence of numbers. Check condition - the number does not exceed 70. Optimize code length to single line.
Number sequence: 21, 31, 44, 61, 1, 40, 65.
Solution
We will solve this task step by step. Let's set the number sequence in the “array” form:
[21, 31, 44, 61, 1, 40, 65]
Add a reference to the every()
method to the “array”:
[21, 31, 44, 61, 1, 40, 65].every();
In the brackets of the method, we will write the arrow function:
[21, 31, 44, 61, 1, 40, 65].every(number => number <= 70);
And put the entire structure in the brackets of the console.log()
method to get the result:
console.log([21, 31, 44, 61, 1, 40, 65].every(number => number <= 70));
Result:
false
Test 3. Check that place values in the words array are less than 21. Use
every()
method and callback function. The array:
let words = [
{word: 'one', number: 1},
{word: 'two', number: 2},
{word: 'nine', number: 9},
{word: 'twelve', number: 12},
{word: 'twenty', number: 20},
{word: 'eleven', number: 11}];
Solution
To use standard methods when you are working with arrays like in this task, you need to specify in the function which part of data will be checked. Therefore, in CompFunc
we will write “num.number”, where num is the received array element, and number is the part of the array that we will check.
let words = [
{word: 'one', number: 1},
{word: 'two', number: 2},
{word: 'nine', number: 9},
{word: 'twelve', number: 12},
{word: 'twenty', number: 20},
{word: 'eleven', number: 11}];
function compFunc(num){
return num.number < 21;
}
console.log(words.every(compFunc));
Result:
true