In all programming languages there comes a time when it is necessary to get acquainted with arrays, to work with them, to process them. There are a lot of various built-in methods to work with arrays, which will allow us to perform all sorts of manipulations with arrays.

For example, in JavaScript there are methods to add or remove elements (push(), pop(), shift(), unshift(), splice(), slice(), concat()), to search among array items (indexOf(), lastIndexOf(), includes(), find(), filter(), findIndex()), as well as to convert an array (map(), sort(), reverse(), split(), join(), reduce()).

In this article, we look at verification methods, specifically some() and every().

Syntax and parameters

The some() method checks whether there is an element in the array that satisfies a particular condition. This condition is passed to the method as a parameter.

The syntax of the method is as follows:

nameOfArray.some ( function ( value, index, array ) , this )

Let's elaborate:

  • function() - a callback function, called gradually for each element in a given array until the end of the array is reached or it returns true, this is actually the condition we are checking for (required parameter);

  • value - a value of the current element in array (required value);

  • index - an index of the current element in array (optional parameter);

  • array - an array the current element belongs to (optional parameter);

  • this - a value passed to the function. If not specified, "undefined" will be used as the value of "this" (optional).

Return value

In general, some() is the boolean method, therefore it can only return two values - either true or false. The some() method checks all items in the array. If for at least one of all elements the given condition is fulfilled the method will return 1 or true. The method will return true if a callout of the nested function also returns the true.

If the method finds a value that satisfies the condition, it immediately returns true and stops working without checking all the following elements. Alternatively, the method will return 0 or false. Logically, if the array is empty, the result will be false.

Note that some() is called only for indexes of the set array which have assigned values. If some index values are deleted or not specified then some() is not called there.

This method does not change the array it was called for, but only checks the elements for a certain condition. And the method will not process items that were added to the array after the method was initiated.

Examples

To get a better grasp of this issue, let's make a few simple examples. To begin with, let's check if there are zero values in a certain array. Firstly, we specify several arrays with different values.

const array_first = [1,2,3,15,-5,-34];
function contains_zero(value)
{
  return value === 0;
}
array_first.some(contains_zero);    //returns false

Now let's write the same functions declaring the function inside the method using a lambda expression:

const array_second = [-1,-2,-3,0,1,3,0];
array_second.some(value => value === 0); //returns true

In this example we use arrow functions. With it we can make functions shorter and clearer. Arrow functions will be useful only if the function is not large and complex. Otherwise it is better to use the classic way of writing a function.

The method can be used not only for numeric arrays, but also for other types. For instance, as a parameter we give an array of string values. Let's set a list of invited guests for an event and later let's check if there is a person in the guest list.

const guests = ['Ivan', 'Mariia', 'Kate', 'Iryna', 'Orest', 'Taras'];
function check(array, value)
{
    return array.some(function(array_value)
    {
        return value === arrayValue;
  });
}
check(quests, 'Orest');     //true
check(quests, 'Vlad');      // false

some() vs every()

Speaking of some() we can not omit every(). They are quite similar and often confused. The every() method accepts the same parameters as the some() method, and similarly can only return true or false.

some() returns trueif at least one of the elements of the array meets a certain condition, while every() returns true if all the elements of the array meet this condition. This is the main difference and we will highlight it in the following example. This time we take a slightly more complex structure as an array.

const guests = [
    {id: 1, name: "Ivan", age: 22},
    {id: 2, name: "Mariia", age: 20},
    {id: 3, name: "Taras", age: 25},
    {id: 4, name: "Kate", age: 19},
    {id: 5, name: "Orest", age: 15}
];
const func_first = guests.some(x => x.age < 18);    //true
const func_second = guests.every(x => x.age > 18);    //false

In this example we mention the guests of a party. We use some() andevery() to check whether there are minors among the invited guests.

In some() we check whether there is someone among the guests who is less than 18 years old. The result will be true, because one person is only 15 years old.

In every() we check whether all invited guests are adults. We already know that there is one guest on the list who is 15 years old. As every() checks all elements, and if at least one of them does not meet the condition, the result will be false. So, in this case the method returns false.

To conclude, if the some() method finds an iteration in which the result is true, the method stops and returns this result. Otherwise it goes to the end, and if it does not find the true result, it will return the false. On the other hand, every() runs throughout the array from start to finish. And only in case if each element meets or does not meet a certain condition we will get the result.