mozilla

Revision 1605 of Array.prototype.every()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/every
  • Revision title: Array every method
  • Revision id: 1605
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment Core JavaScript 1.5 Reference:Objects:Array:every moved to Core JavaScript 1.5 Reference:Global Objects:Array:every

Revision Content

Summary

Tests whether all elements in the array pass the test implemented by the provided function.

Method of Array
Implemented in: JavaScript 1.5 (Gecko 1.8b2 and later)
ECMA Version: none

Syntax

every(callback{{mediawiki.external(', <i>thisObject</i>')}})

Parameters

callback 
Function to test for each element.
thisObject 
Object to use as this when executing callback.

Description

every executes the provided function (callback) once for each element present in the array until it finds one where callback returns a false value. If such an element is found, the test aborts and false is returned, otherwise (callback returned a true value for each of the elements) every will return true. Arrays are treated as "dense", meaning that callback will be invoked for each index less than the array's length, even if a given index has never been explicitly assigned.

callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

If a thisObject parameter is provided to every, it will be used as the this for each invocation of the callback. If it is not provided, or is null, the global object associated with callback is used instead.

every does not mutate the array on which it is called.

The range of elements processed by every is set before the first invocation of callback. Elements which are appended to the array after the call to every begins will not be visited by callback. If existing elements of the array are changed, or deleted, their value as passed to callback will be the value at the time every visits them; elements that are deleted will have the value undefined.

Examples

Example: Testing size of all array elements

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

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

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Tests whether all elements in the array pass the test implemented by the provided function.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.5 (Gecko 1.8b2 and later)</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>none</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
every(<i>callback</i>{{mediawiki.external(', &lt;i&gt;thisObject&lt;/i&gt;')}})
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>callback</code> </dt><dd> Function to test for each element.
</dd><dt> <code>thisObject</code> </dt><dd> Object to use as <code>this</code> when executing <code>callback</code>.
</dd></dl>
<h3 name="Description"> Description </h3>
<p><code>every</code> executes the provided function (<code>callback</code>) once for each element present in the array until it finds one where <code>callback</code> returns a false value.  If such an element is found, the test aborts and <code>false</code> is returned, otherwise (<code>callback</code> returned a true value for each of the elements) <code>every</code> will return <code>true</code>.  Arrays are treated as "dense", meaning that <code>callback</code> will be invoked for each index less than the array's <code>length</code>, even if a given index has never been explicitly assigned.
</p><p><code>callback</code> is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.
</p><p>If a <code>thisObject</code> parameter is provided to <code>every</code>, it will be used as the <code>this</code> for each invocation of the <code>callback</code>.  If it is not provided, or is <code>null</code>, the global object associated with <code>callback</code> is used instead.
</p><p><code>every</code> does not mutate the array on which it is called.
</p><p>The range of elements processed by <code>every</code> is set before the first invocation of <code>callback</code>.  Elements which are appended to the array after the call to <code>every</code> begins will not be visited by <code>callback</code>.  If existing elements of the array are changed, or deleted, their value as passed to <code>callback</code> will be the value at the time <code>every</code> visits them; elements that are deleted will have the value <code>undefined</code>.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Testing_size_of_all_array_elements"> Example: Testing size of all array elements </h4>
<p>The following example tests whether all elements in the array are bigger than 10.
</p>
<pre>function isBigEnough(element, index, array) {
  return (element &gt;= 10);
}
passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
</pre>
Revert to this revision