JavaScript Arrays: some and every Methods

JavaScript's some and every methods, introduced in ECMAScript 5, inspect the elements of an array to determine whether some or all of their values match criteria you specify. They work in a manner similar to the ECMAScript 5 iteration functions: a callback function is passed as the first argument; each element of the array is passed in turn to this function and evaluated according to the criteria established there. The callback function accepts three arguments: the current array element, its index, and the array itself.

The some and every methods return true or false to indicate whether some or all of the elements in the array they are invoked on meet the criteria established in the callback function.

The every Method

First we will demonstrate the every method. We want to determine whether all of the values in our example arrays are numeric. We define a callback function, checkNumeric, which returns true if the element passed to it contains a numeric value.[1] We pass this function as we invoke the every method on two example arrays; one contains some non-numeric values while the second consists only of numbers:

// example callback function
function checkNumeric(el, i, ar) {
    return typeof el === 'number';
}

// example array with some non-numeric values
var ar = [2, 27, 33.45, true, 'yes', 0, 3.14];
console.log( ar.every(checkNumeric) ); // false

// example array with numbers only
var ar2 = [1, 2, 3, 4.4];
console.log( ar2.every(checkNumeric) ); // true

When we invoke the every method on the first example array, it returns false since it contains some non-numeric values. When we invoke the every method on the second array, it returns true since it contains only numeric values.

If each element of the array satisfies the criteria, the every method returns true. As soon as one of the elements does not match the criteria, the method returns false.

The some Method

The some method returns true as soon as any element satisfies the criteria established by the callback function. If none of the elements match the criteria, the method returns false.

Below we demonstrate using the some method to check whether any of the elements in our example arrays contain non-numeric values. We define a function, checkNaN, that returns true if the element passed to it contains a value that is not a number. We pass this function as we invoke the some method on two example arrays; one contains some non-numeric values while the second consists only of numbers:

// example callback function
function checkNaN(el, i, ar) {
    return isNaN(el);
}

// example array with some non-numeric values
var ar = [2, 27, 33.45, true, 'yes', 0, 3.14];
console.log( ar.some(checkNaN) ); // true

// example array with numbers only
var ar2 = [1, 2, 3, 4.4];
console.log( ar2.some(checkNaN) ); // false

When we invoke the some method on the first example array, it returns true since it contains some non-numeric values. When we invoke the some method on the second array, it returns false since it contains only numeric values.

Back to top


  1. As mentioned above, the callback function accepts three arguments. We only need the first for this example ^