---
title: Array.prototype.every()
slug: Web/JavaScript/Reference/Global_Objects/Array/every
tags:
  - Array
  - ECMAScript 5
  - JavaScript
  - Method
  - Prototype
  - polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Array/every
---
<div>{{JSRef}}</div>

<p><span class="seoSummary"><strong><code>every()</code></strong> メソッドは、列内のすべての要素が指定された関数で実装されたテストに合格するかどうかをテストします。これは論理値を返します。</span></p>

<div>{{EmbedInteractiveExample("pages/js/array-every.html","shorter")}}</div>

<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、<a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate"><var>arr</var>.every(<var>callback</var>(<var>element</var>[, <var>index</var>[, <var>array</var>]])[, <var>thisArg</var>])</pre>

<h3 id="Parameters" name="Parameters">引数</h3>

<dl>
 <dt><code><var>callback</var></code></dt>
 <dd>各要素に対してテストを実行する関数です。次の 3 つの引数を取ります。
 <dl>
  <dt><code><var>element</var></code></dt>
  <dd>現在処理されている要素です。</dd>
  <dt><code><var>index</var></code> {{Optional_inline}}</dt>
  <dd>現在処理されている要素の添字です。</dd>
  <dt><code><var>array</var></code> {{Optional_inline}}</dt>
  <dd><code>every</code> が実行されている配列です。</dd>
 </dl>
 </dd>
 <dt><code><var>thisArg</var></code> {{Optional_inline}}</dt>
 <dd><code><var>callback</var></code> を実行するときに <code>this</code> として使用すされる値です。</dd>
</dl>

<h3 id="Return_value" name="Return_value">返値</h3>

<p><code><var>callback</var></code> 関数が配列のすべての要素について{{Glossary("truthy", "真値")}}を返した場合は <strong><code>true</code></strong>。それ以外は <strong><code>false</code></strong>。</p>

<h2 id="Description" name="Description">解説</h2>

<p><code>every</code> は、与えられた <code><var>callback</var></code> 関数を、配列に含まれる各要素に対して一度ずつ、<code><var>callback</var></code> が{{Glossary("falsy", "偽値")}}を返す要素が見つかるまで呼び出します。そのような要素が見つかると、<code>every</code> メソッドはただちに <code>false</code> を返します。<code><var>callback</var></code> がすべての要素に対して{{Glossary("truthy", "真値")}}を返した場合、<code>every</code> は <code>true</code> を返します。</p>

<div class="blockIndicator note">
<p><strong>注意</strong>: このメソッドを空の配列に対して呼び出すと、無条件に <code>true</code> を返します。</p>
</div>

<p><code><var>callback</var></code> は値が代入されている配列の要素に対してのみ呼び出されます。つまり、すでに削除された要素や、まだ値が代入されていない要素に対しては呼び出されません。</p>

<p><code><var>callback</var></code> は、要素の値、要素の添字、走査されている Array オブジェクトという 3 つの引数をともなって呼び出されます。</p>

<p><code><var>thisArg</var></code> 引数が <code>every</code> に与えられると、それがコールバックの <code>this</code> として使用されます。それ以外の場合は <code>undefined</code> が <code>this</code> の値として使われます。<code><var>callback</var></code> が最終的に監視できる <code>this</code> の値は、<a href="/ja/docs/Web/JavaScript/Reference/Operators/this">関数から見た <code>this</code> の決定に関する一般的なルール</a>によって決定されます。</p>

<p><code>every</code> は呼び出された配列を変化させません。</p>

<p><code>every</code> によって処理される要素の範囲は、<code><var>callback</var></code> が最初に呼び出される前に設定されます。<code><var>callback</var></code> は、<code>every</code> の呼び出しが開始された後に追加された要素に対しては、実行されません。既存の配列要素が変更されたり、削除された場合、<code><var>callback</var></code> に渡される値は <code>every</code> がそれらを訪れた時点での値になり、<code>every</code> が削除された要素を訪問することはありません。</p>

<p><code>every</code> は数学における「∀ (すべての / for all)」記号と同様のふるまいをします。具体的に言うと、空の配列に対しては <code>true</code> を返します。(<a href="https://en.wikipedia.org/wiki/Empty_set#Properties">空集合</a>のすべての要素が与えられた任意の条件を満たすことは<a href="https://en.wikipedia.org/wiki/Vacuous_truth">空虚に真</a>です。)</p>

<h2 id="Polyfill" name="Polyfill">ポリフィル</h2>

<p><code>every</code> は ECMA-262 標準に第5版で追加されたもので、この標準のそれ以外の実装には存在しないかもしれません。これを回避するには、スクリプトの最初に以下のコードを挿入することで、ネイティブで対応していない実装でも <code>every</code> を使用できるようにすることができます。</p>

<p>このアルゴリズムは、<code>Object</code> と <code>TypeError</code> が元の値を持ち、<code><var>callbackfn</var>.call</code> が {{jsxref("Function.prototype.call")}} の元の値に評価されると仮定するもので、ECMA-262 第5版で指定されているものと全く同じです。</p>

<pre class="brush: js notranslate">if (!Array.prototype.every) {
  Array.prototype.every = function(callbackfn, thisArg) {
    'use strict';
    var T, k;

    if (this == null) {
      throw new TypeError('this is null or not defined');
    }

    // 1. Let O be the result of calling ToObject passing the this
    //    value as the argument.
    var O = Object(this);

    // 2. Let lenValue be the result of calling the Get internal method
    //    of O with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = O.length &gt;&gt;&gt; 0;

    // 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
    if (typeof callbackfn !== 'function' &amp;&amp; Object.prototype.toString.call(callbackfn) !== '[object Function]') {
      throw new TypeError();
    }

    // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
    if (arguments.length &gt; 1) {
      T = thisArg;
    }

    // 6. Let k be 0.
    k = 0;

    // 7. Repeat, while k &lt; len
    while (k &lt; len) {

      var kValue;

      // a. Let Pk be ToString(k).
      //   This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the HasProperty internal
      //    method of O with argument Pk.
      //   This step can be combined with c
      // c. If kPresent is true, then
      if (k in O) {
        var testResult;
        // i. Let kValue be the result of calling the Get internal method
        //    of O with argument Pk.
        kValue = O[k];

        // ii. Let testResult be the result of calling the Call internal method
        // of callbackfn with T as the this value if T is not undefined
        // else is the result of calling callbackfn
        // and argument list containing kValue, k, and O.
        if(T) testResult = callbackfn.call(T, kValue, k, O);
        else testResult = callbackfn(kValue,k,O)

        // iii. If ToBoolean(testResult) is false, return false.
        if (!testResult) {
          return false;
        }
      }
      k++;
    }
    return true;
  };
}
</pre>

<h2 id="Examples" name="Examples">例</h2>

<h3 id="Testing_size_of_all_array_elements" name="Testing_size_of_all_array_elements">すべての配列要素の大きさをテストする</h3>

<p>次の例は、配列内のすべての要素が 10 よりも大きいかどうかテストします。</p>

<pre class="brush: js notranslate">function isBigEnough(element, index, array) {
  return element &gt;= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough);   // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
</pre>

<h3 id="Using_arrow_functions" name="Using_arrow_functions">アロー関数の使用</h3>

<p><a href="/ja/docs/Web/JavaScript/Reference/Functions/Arrow_functions">アロー関数</a>はより短い構文で同じテストを提供します。</p>

<pre class="brush: js notranslate">[12, 5, 8, 130, 44].every(x =&gt; x &gt;= 10);   // false
[12, 54, 18, 130, 44].every(x =&gt; x &gt;= 10); // true</pre>

<h3 id="初期配列への影響_変更、追加、削除">初期配列への影響 (変更、追加、削除)</h3>

<p>次の例は、配列が変更されたときに <code>every</code> メソッドの動作をテストするものです。</p>

<pre class="brush: js notranslate">// ---------------
// Modifying items
// ---------------
let arr = [1, 2, 3, 4];
arr.every( (elem, index, arr) =&gt; {
  arr[index+1] -= 1
  console.log(`[${arr}][${index}] -&gt; ${elem}`)
  return elem &lt; 2
})

// Loop runs for 3 iterations, but would
// have run 2 iterations without any modification
//
// 1st iteration: [1,1,3,4][0] -&gt; 1
// 2nd iteration: [1,1,2,4][1] -&gt; 1
// 3rd iteration: [1,1,2,3][2] -&gt; 2

// ---------------
// Appending items
// ---------------
arr = [1, 2, 3];
arr.every( (elem, index, arr) =&gt; {
  arr.push('new')
  console.log(`[${arr}][${index}] -&gt; ${elem}`)
  return elem &lt; 4
})

// Loop runs for 3 iterations, even after appending new items
//
// 1st iteration: [1, 2, 3, new][0] -&gt; 1
// 2nd iteration: [1, 2, 3, new, new][1] -&gt; 2
// 3rd iteration: [1, 2, 3, new, new, new][2] -&gt; 3

// ---------------
// Deleting items
// ---------------
arr = [1, 2, 3, 4];
arr.every( (elem, index, arr) =&gt; {
  arr.pop()
  console.log(`[${arr}][${index}] -&gt; ${elem}`)
  return elem &lt; 4
})

// Loop runs for 2 iterations only, as the remaining
// items are `pop()`ed off
//
// 1st iteration: [1,2,3][0] -&gt; 1
// 2nd iteration: [1,2][1] -&gt; 2</pre>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.prototype.every', 'Array.prototype.every')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div>
<p>{{Compat("javascript.builtins.Array.every")}}</p>
</div>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{jsxref("Array.prototype.forEach()")}}</li>
 <li>{{jsxref("Array.prototype.some()")}}</li>
 <li>{{jsxref("Array.prototype.find()")}}</li>
 <li>{{jsxref("TypedArray.prototype.every()")}}</li>
</ul>