Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array every() Method

JavaScript Array every() Method

Array Object Reference JavaScript Array object

Example

Check the array ages whether all of its elements are greater than or equal to 18:

var

ages = [

32

,

33

,

16

,

40

];

function

checkAdult(age) {

   

return

age >=

18

;

}

function

myFunction() {

    document.getElementById(

"demo"

).innerHTML = ages.every(checkAdult);

}

The output result is:

false

Try it »


Definition and Usage

The every() method checks whether all elements in an array pass a specified condition (provided by a function).

The every() method uses the specified function to test all elements in the array:

Note: every() does not check empty arrays.

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


Browser support

The numbers in the table indicate the first browser version that supports this method.

Method
every() Yes 9 1.5 Yes Yes

Syntax


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

Parameter Description

Parameters Description
function(currentValue, index,arr) Required. A function that is executed for each element in the array
 函数参数:       
   
Parameter Description   currentValue Required. The value of the current element   index Optional. The index of the current element   arr Required. The array object to which the current element belongs | Parameter | Description | *currentValue* | Required. The value of the current element | *index* | Optional. The index of the current element | *arr* | Optional. The array object to which the current element belongs | | Parameter | Description | | *currentValue* | Required. The value of the current element | | *index* | Optional. The index of the current element | | *arr* | Optional. The array object to which the current element belongs | | *thisValue* | Optional. An object passed to the function as the value of "this" when the callback is executed. If thisValue is omitted, the value of "this" is "undefined" |

Technical Details

Return Value: Boolean. Returns true if all elements pass the test, otherwise returns false.
JavaScript version: 1.6

More examples

Example

Check whether all elements of the array ages are greater than or equal to the number specified in the input box:

Minimum age: "number"

id=

"ageToCheck"

value=

"18"