JavaScript Array every()

Posted by

JavaScript Array every() Method

The JavaScript Array.prototype.every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

arr.every(callback(element[, index[, array]])[, thisArg])

[/dm_code_snippet]

Parameters:

  • callback: Function to test for each element, taking three arguments:
    • element: The current element being processed in the array.
    • index (Optional): The index of the current element being processed in the array.
    • array (Optional): The array every() was called upon.
  • thisArg (Optional): Value to use as this when executing callback.

Description

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

The every() method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value (a value that becomes false when converted to a Boolean). If such an element is found, the every() method immediately returns false. Otherwise, if callback returns a truthy value for all elements, every() returns true.

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

If a thisArg parameter is provided to every(), it will be used as callback‘s this value. Otherwise, the value undefined will be used as its this value. The this value ultimately observable by callback is determined according to the usual rules for determining the this seen by a function.

Examples

Using every() to check all elements in an array are larger than 10:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

function isBigEnough(element, index, array) {
  return element >= 10;
}

[12, 5, 8, 130, 44].every(isBigEnough);   // false
[12, 54, 18, 130, 44].every(isBigEnough);  // true

[/dm_code_snippet]

Using every() with arrow function syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

[12, 5, 8, 130, 44].every(x => x >= 10);  // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true

[/dm_code_snippet]

Browser Compatibility

The every() method is supported in all major browsers, including Internet Explorer 9 and higher.

Polyfill

If you need to support older browsers (like IE8 and below) you can use the following polyfill:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisArg */)
  {
    'use strict';

    if (this == null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun != 'function')
      throw new TypeError();

    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
    for (var i = 0; i < len; i++)
    {
      if (i in t && !fun.call(thisArg, t[i], i, t))
        return false;
    }

    return true;
  };
}

[/dm_code_snippet]

Further Reading