totn JavaScript

JavaScript: Array every() method

This JavaScript tutorial explains how to use the Array method called every() with syntax and examples.

Description

In JavaScript, every() is an Array method that is used to return a Boolean value indicating whether every element in an array satisfies a criteria provided. Because the every() method is a method of the Array object, it must be invoked through a particular instance of the Array class.

Syntax

In JavaScript, the syntax for the every() method is:

array.every(callback(element [, index [, array]]) [,thisParameter]);

Parameters or Arguments

callback
A callback function to test each element of the array.
element
The current element of the array.
index
Optional. The index of the current element within the array.
array
Optional. A reference to the original array.
thisParameter
Optional. A parameter that will be used as this within the callback function.

Returns

The every() method returns true if all elements in the array satisfy the criteria provided.

Once the every() method encounters an element is the array that does not satisfy the criteria provided, it returns false and does not process any further elements in the array.

Note

  • The every() method does not modify the original array.

Example

Let's take a look at an example of how to use the every() method in JavaScript.

For example:

var totn_array = [ 1, 5, 10, 15 ];

function greater_than_zero(totn_element) {
   return totn_element > 0;
}

window.console.log(totn_array.every(greater_than_zero));

In this example, we have declared an array object called totn_array that has 4 elements. We have then invoked the every() method of the totn_array variable to test each element value using the callback function greater_than_zero.

We have written the output of the every() method to the web browser console log, for demonstration purposes, to show what the every() method returns.

The following will be output to the web browser console log:

true

In this example, the every() method will return true because all of the elements in the array are greater than zero.

Example that returns a false value

Next, let's modify our array to include an element that has a negative value such as -1.

For example:

var totn_array = [ 1, 5, -1, 10, 15 ];

function greater_than_zero(totn_element) {
   return totn_element > 0;
}

window.console.log(totn_array.every(greater_than_zero));

The following will be output to the web browser console log:

false

Since there is now an element in the array that is NOT greater than zero, the every() method will return false.

Using the thisParameter

When you use the optional parameter called thisParameter, the every() method will provide the value of thisParameter as this within the callback function.

For example:

var totn_array = [ 1, 5, -1, 10, 15 ];
var msg = " is the element being tested";

function greater_than_zero(totn_element) {
   window.console.log(totn_element + this);
   return totn_element > 0;
}

window.console.log(totn_array.every(greater_than_zero, msg));

In this example, we have used thisParameter to provide the following string value as this in the callback function:

" is the element being tested"

The following will be output to the web browser console log:

1 is the element being tested
5 is the element being tested
-1 is the element being tested
false

In this case, the every() method will return false because the third element in the array is -1 which is not greater than 0. Once the every() method encounters an element that does not satisfy the criteria, it immediately returns false and does not process the rest of the elements in the array (preventing the every() method from processing the elements 10 and 15 in the array). This is seen by the output to the web browser console log.