The Array method every() in Apps Script
The Array method every()
is used to check if every value in an array meets a set of conditions.
Prerequisites
This tutorial assumes that you're familiar with:
Syntaxarray.every(callbackFunction)
Parameters
array.every(callbackFunction)
callbackFunction (required):
This is the function that will be executed once for every element in the array. This function is where you need to specify the conditions that need to be checked. If all the conditions are met, this function should return
true
. If one or more conditions are not met, it should returnfalse
.
The callbackFunction
can accept three arguments:
value
(required): The current value being processed.index
(optional): The index of the value in the array.array
(optional): The original array that theevery()
method was called on.
Return value
The every()
method returns true
if every element in the array meets the conditions specified in the callback function and false
otherwise.
How does the every() Array method work?
The every()
method will execute the callbackFunction
once for every value in the array. If the callbackFunction
returns false
(or a value that is treated as false by Apps Script), the every()
method will immediately return false
. If the callbackFunction
returns true
(or a value that is treated as true
by Apps Script) for every value in the array, the every()
method will return true
.
Note: The following values in Apps Script are treated as false
:
false
: The boolean value false0
: The number 0""
: An empty stringnull
: The null valueundefined
: An undefined valueNaN
: A value that represents "not a number"
Other values are treated as true
by Apps Script. Interestingly, even empty arrays and empty objects are treated as true
by Apps Script.
ExamplesExample 1: Using the every() Array method to check if every element in an array is a number
Example 1.1
In the code below, the function isNumber(value)
is the callbackFunction parameter in the every()
method. This function is executed once for every element in the array values
and it returns true
if the element is a number and false
otherwise. Since every element is a number, the every()
method returns true.
function everyEx1_1() {
var values = [1, -3.4, 27.1, 1000, 0];
Logger.log(values.every(isNumber));
}
function isNumber(value) {
return typeof value === 'number'
}
Output
true
How does the above code work?
The every()
method will execute the callback function once for every element in the array values
. If the callback function returns false
, the every()
method will immediately return false
. If the callback function returns true
for every element, the every()
method will return true
.
Therefore, the function isNumber()
will be called 5 times, once for every element in the array values
.
isNumber(1); //returns true
isNumber(-3.4); //returns true
isNumber(27.1); //returns true
isNumber(1000); //returns true
isNumber(0); //returns true
Since true
is returned in all 5 cases, the every()
method returns true
.
Example 1.2
Instead of defining the function isNumber()
separately and then passing it as a parameter, you can also define it in-line. The code below shows you how to do this.
function everyEx1_2() {
var values = [1, -3.4, 27.1, 1000, 0];
var output = values.every(function(value) {
return typeof value === 'number'
});
Logger.log(output);
}
Output
true
Note
Notice that there is no name for the function in the above example.
This is because, in Apps Script (which is a version of JavaScript), a function is basically a variable with a value of type function.
Consider the function addTwoNumbers()
below.
function addTwoNumbers(num1, num2) {
return num1 + num2;
}
This function can also be rewritten in the following way:
var addTwoNumbers = function(num1, num2) {
return num1 + num2;
}
Here the variable addTwoNumbers
contains a value of type function. In Apps Script, both of the above code snippets have the exact same result.
When you pass a function as a parameter to the every()
method, you're really passing the function value. Therefore, the following statement values.every(isNumber);
in Example 1.1 is the same as the code snippet below in Example 1.2
values.every(function(value) {
return typeof value === 'number'
});
This is because isNumber
is just a variable that contains the following function value:
function(value) {
return typeof value === 'number'
}
Example 2: The every() function will immediately return false when the callBack() function returns false the first time
The every()
array method will execute the callback function once for every value in the array until it encounters a value for which the callback function returns false
. As soon as the every()
method encounters a value that does not meet the conditions set forth in the callback function, the every()
method will return false and won't process the remaining elements in the array.
In the code below, the isNumberLog()
function will return false
for the value "3"
since it is not a number. The every()
method will return false after it processes the value "3"
and the remaining values 4, 5 and 6 will not be processed.
function everyEx2() {
var values = [1, 2, "3", 4, 5, 6];
Logger.log(values.every(isNumberLog));
}
function isNumberLog(value) {
Logger.log(value);
return typeof value === 'number'
}
Output
1.0
2.0
3
false
Example 3: Empty arrays and objects are treated as "true" values in Apps Script
In the code below, the array values contains only empty arrays and objects and the callback function simply returns the value passed to it. Since Apps Script treats empty arrays and objects as a true
value, the callback function returns true
for all four array elements. The every()
array method therefore returns true and this value is logged.
function everyEx3() {
var values = [[], [], {}, {}];
Logger.log(values.every(function(value) {
return value;
}));
}
Output
true
Conclusion
In this tutorial, you learned how to use the every()
Array method to check if every element in the array meets a set of conditions.