Nodejs Array Every every(fun /*, thisp*/)

Here you can find the source of every(fun /*, thisp*/)

Method Source Code

/**/*  w w w  .j a va  2 s  . c  o m*/
 * Invokes `fun` on each element of the array and returns true if every
 * invocation of `fun` returns true or returns a truthy value.  Otherwise
 * returns false.
 *
 * The first argument given to `fun` is a single array element and the second
 * argument is the index of that element in the array.
 *
 * This definition is compatible with the JavaScript 1.6 definition for
 * `Array#every` in Spidermonkey.
 *
 * This implementation comes from:
 * https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every
 *
 * @function
 * @param   {Function}  fun     predicate function that will be applied to each
 * array element
 * @param   {Object}    [thisp] context in which `fun` will be invoked - `this`
 * in `fun` will refer to `thisp`
 * @returns {Boolean}   true if `fun` returned true for every array element, false otherwise
 */
Array.prototype.every = Array.prototype.every || function(fun /*, thisp*/)  {
    var len = this.length >>> 0;
    if (typeof fun != "function") {
        throw new TypeError();
    }

    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
        if (i in this &&
            !fun.call(thisp, this[i], i, this)) {
            return false;
        }
    }

    return true;
};

Related

  1. every(fn, context)
    Array.prototype.every = function(fn, context) {
      if (typeof fn != "function") {
        throw new TypeError(fn + " is not a function");
      if (typeof context === 'undefined') {
        context = this;
      for (var i = 0, l = this.length; i < l; i++) {
        if (this.hasOwnProperty(i) && !fn.call(context, this[i], i, this)) {
    ...
    
  2. every(fun /*, thisp */)
    Array.prototype.every = Array.prototype.every || function(fun ) {
      "use strict";
      if (this === void 0 || this === null) throw new TypeError();
      var t = Object(this);
      var len = t.length >>> 0;
      if (typeof fun !== "function") throw new TypeError();
      var thisp = arguments[1];
      for (var i = 0; i < len; i++) {
        if (i in t && !fun.call(thisp, t[i], i, t)) return false;
    ...
    
  3. every(isOk)
    const array = [1, 42, 7, 9, 13, 10, 20, 35, 45, -7, -3, 0, 4, -1, 15];
    Array.prototype.every = function(isOk) {
      const len = this.length;
      for(let i = 0; i < len; i += 1) {
        console.log(isOk(this[i], i, this));
        if(!isOk(this[i], i, this)) {
          return false;
      return true;
    };
    Array.prototype.some = function(isOk) {
      const len = this.length;
      for(let i = 0; i < len; i += 1) {
        if(isOk(this[i], i, this)) {
          return true;
      return false;
    };
    console.log(array.some(x => x < 10));
    console.log(array.some(x => x > 100));
    console.log([1,2,3].some(x => x < 10));
    function isIncreasing(x, i, arr) {
      if(i === 0) {
        return true;
      return arr[i - 1] < x;
    console.log([1,2,3].every(isIncreasing));
    console.log([42, 1, 2, 3].every((x, i) => x % 2 === i % 2));
    
  4. every(iterator)
    Array.prototype.every = function(iterator) {
      for (var i = 0, len = this.length; i < len; i++) {
        if (!iterator(this[i], i)) {
          return false;
      return true;
    };
    
  5. everySyncasync (callback, thisArg)
    Array.prototype.everySync = async function (callback, thisArg) {
      for (let [index, item] of Object.entries(this)) {
        if (!await callback(item, index, this)) return false
      return true
    
  6. every(callback)
    Array.prototype.every = function every(callback) {
      if (typeof callback !== 'function') {
        throw new TypeError(callback + ' is not a function');
      for (var array = this, index = 0, length = array.length; index < length; ++index) {
        if (index in array && !callback.call(arguments[1], array[index], index, array)) {
          return false;
      return true;
    };