Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.every()

BaselineWidely available

Theevery() method ofArray instances tests whetherall elements in the array pass the test implemented by the provided function. Itreturns a Boolean value.

Try it

const isBelowThreshold = (currentValue) => currentValue < 40;const array1 = [1, 30, 39, 29, 10, 13];console.log(array1.every(isBelowThreshold));// Expected output: true

Syntax

js
every(callbackFn)every(callbackFn, thisArg)

Parameters

callbackFn

A function to execute for each element in the array. It should return atruthy value to indicate the element passes the test, and afalsy value otherwise. The function is called with the following arguments:

element

The current element being processed in the array.

index

The index of the current element being processed in the array.

array

The arrayevery() was called upon.

thisArgOptional

A value to use asthis when executingcallbackFn. Seeiterative methods.

Return value

true unlesscallbackFn returns afalsy value for an array element, in which casefalse is immediately returned.

Description

Theevery() method is aniterative method. It calls a providedcallbackFn function once for each element in an array, until thecallbackFn returns afalsy value. If such an element is found,every() immediately returnsfalse and stops iterating through the array. Otherwise, ifcallbackFn returns atruthy value for all elements,every() returnstrue. Read theiterative methods section for more information about how these methods work in general.

every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returnstrue. (It isvacuously true that all elements of theempty set satisfy any given condition.)

callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots insparse arrays.

Theevery() method isgeneric. It only expects thethis value to have alength property and integer-keyed properties.

Examples

Testing size of all array elements

The following example tests whether all elements in the array are 10 or bigger.

js
function isBigEnough(element, index, array) {  return element >= 10;}[12, 5, 8, 130, 44].every(isBigEnough); // false[12, 54, 18, 130, 44].every(isBigEnough); // true

Check if one array is a subset of another array

The following example tests if all the elements of an array are present in another array.

js
const isSubset = (array1, array2) =>  array2.every((element) => array1.includes(element));console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 7, 6])); // trueconsole.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 8, 7])); // false

Using the third argument of callbackFn

Thearray argument is useful if you want to access another element in the array. The following example first usesfilter() to extract the positive values and then usesevery() to check whether the array is strictly increasing.

js
const numbers = [-2, 4, -8, 16, -32];const isIncreasing = numbers  .filter((num) => num > 0)  .every((num, idx, arr) => {    // Without the arr argument, there's no way to easily access the    // intermediate array without saving it to a variable.    if (idx === 0) return true;    return num > arr[idx - 1];  });console.log(isIncreasing); // true

Using every() on sparse arrays

every() will not run its predicate on empty slots.

js
console.log([1, , 3].every((x) => x !== undefined)); // trueconsole.log([2, , 2].every((x) => x === 2)); // true

Calling every() on non-array objects

Theevery() method reads thelength property ofthis and then accesses each property with a nonnegative integer key less thanlength until they all have been accessed orcallbackFn returnsfalse.

js
const arrayLike = {  length: 3,  0: "a",  1: "b",  2: "c",  3: 345, // ignored by every() since length is 3};console.log(  Array.prototype.every.call(arrayLike, (x) => typeof x === "string"),); // true

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.every

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp