Latest web development tutorials

JavaScript Array every () method

Array Object Reference JavaScript Array Object

Examples

Detecting whether all elements of an array of ages greater than 18:

var ages = [32, 33, 16, 40];

function checkAdult (age) {
return age> = 18;
}

function myFunction () {
document.getElementById ( "demo") .innerHTML = ages.every (checkAdult);
}

The output is:

false

try it"

Definition and Usage

every () method for detecting whether an array of all elements that meet the specified conditions (provided by the function).

every () method with the specified function detects all the elements in the array:

  • If the array has detected an element is not satisfied, the entire expression returns false, and the remaining elements will no longer be detected.
  • If all elements satisfy the condition returns true.

Note: every () does not detect an empty array.

Note: every () does not change the original array.


Browser Support

Figures in the table represent the first browser to support the method version number.

method
every () Yes 9 1.5 Yes Yes

grammar

array.every(function(currentValue,index,arr), thisValue)

Parameter Description

parameter description
function (currentValue, index, arr) have to. Function, each element of the array will perform this function function parameters:
parameter description
currentValue have to. The value of the current element
index Optional. The index value of the current element
arr Optional. An array of objects belonging to the current element
thisValue Optional. As the callback object use, passed to the function, it is used as "this" value.
If you omit thisValue, "this" value "undefined"

technical details

return value: Boolean value. If all elements by detecting return true, otherwise returns false.
JavaScript version: 1.6

More examples

Examples

Detecting whether all the elements of an array of ages greater than the specified input box numbers:

<p> Minimum age: <input type = "number" id = "ageToCheck" value = "18"> </ p>
<button onclick = "myFunction () "> point I </ button>

<p> Are all ages are symbols condition? <span id = "demo" > </ span> </ p>

<Script>
var ages = [32, 33, 12, 40];

function checkAdult (age) {
return age> = document.getElementById ( "ageToCheck ") .value;
}

function myFunction () {
document.getElementById ( "demo") .innerHTML = ages.every (checkAdult);
}
</ Script>

try it"

Array Object Reference JavaScript Array Object