Using Array every() method to coding in Javascript/JS

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

Description

In JavaScript, every() is an Array technique that is used to return a Boolean fee indicating whether or not each component in an array satisfies a standards provided. Because the every() technique is a method of the Array object, it need to be invoked thru a specific occasion of the Array class.

Syntax

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

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

Parameters or Arguments

callback A callback function to test each factor of the array. element The contemporary issue of the array. index Optional. The index of the modern-day aspect within the array. array Optional. A reference to the unique array. thisParameter Optional. A parameter that will be used as this within the callback function.

Returns

The every() technique returns real if all factors in the array fulfill the criteria provided.

Once the every() method encounters an factor is the array that does no longer fulfill the criteria provided, it returns false and does now not manner any similarly factors in the array.

Note

The every() method does no longer modify the original array.

Example

Let’s take a look at an example of how to use the every() technique 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() technique of the totn_array variable to test each issue price using the callback feature greater_than_zero.

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

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

true

In this example, the every() approach will return genuine due to the fact 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 issue that has a poor 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 internet browser console log:

false

Since there is now an issue in the array that is NOT higher than zero, the every() technique will return false.

Using the thisParameter

When you use the elective parameter referred to as thisParameter, the every() method will provide the value of thisParameter as this inside 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 furnish the following string cost as this in the callback function:

" is the element being tested"

The following will be output to the internet 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() technique will return false due to the fact the third factor in the array is -1 which is no longer larger than zero Once the every() approach encounters an thing that does now not fulfill the criteria, it right away returns false and does no longer system the rest of the factors in the array (preventing the every() technique from processing the elements 10 and 15 in the array). This is viewed by way of the output to the net browser console log.