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.

Syntax

array.every(callbackFunction)

Parameters

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 return false.

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 the every() 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.

Examples

Example 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

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

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.

Sign up to be notified when I publish new content

By signing up you agree to the Privacy Policy & Terms.