Javascript array some() function is executed on an array and is used to check if array elements meet some condition.
Examples of such condition is as to check if all array elements are greater than 20(for numeric array elements), all array elements are of three characters(if elements are string type), if there are no duplicate elements in the array etc.

Following points should be understood regarding some() function before going into the examples.

  • some() function is called on an array as arr.some().
  • some() function returns true or false values.
  • some() function takes a function as argument. This function is a callback function and should return true or false.
  • some() calls the argument function once on every element of the array till the argument function returns false.
  • Once the argument function returns true for an array element, some() does not call it for other array elements and returns true.
  • If the argument function returns false for all array elements, some() also returns false.

Syntax
As stated above, some() function is called on an array and it takes a function as argument. Thus, its syntax is

 

array.some(argument function);

Here the argument function is a javascript function which should return true or false and it takes the following arguments.
value: This is the first argument which is the current array element. The argument function is called on every array element.
Whenever it is called, an array element is supplied to it. This argument is required.
index: This is the index of the array element that is passed to the function. When this function is called for the first time, index will be 0.
In second time, it will be 1 and so on. This argument is optional.
array: This is the array on which the function is called. This is also optional.
Thus, the complete syntax of some function can also be written as

array.some(function(value, index, array));

Remember that if you supply a single argument to the callback function, then it is considered as the array value.
If two arguments are supplied, then they are considered as array value and its index.
Finally, three arguments will be considered as value, index and array respectively.

Examples
Practical usage of some function can be easily understood by the following examples.

1. Check if any array element is more than 3 characters.

let words = ["of", "to", "into", "in"];

// argument function
function test(item) {
   return item.length > 3;
}

let result = words.some(test);
document.write(result); // will print true

Above code checks for the presence of a lengthy string in the array. The argument function supplied to some accepts only one argument.
This argument is the array element in each function call. test function is called for every array element till it returns true.
Thus, it will be called for the first three array elements and not for the fourth one.

2. Test if the array elements are greater than a value

let ages = [12, 22, 5, 45, 25];

// argument function
function test(age) {
   return age > 50;
}

let result = ages.some(test);
document.write(result); // will print false

Above example checks if the array contains an element which is greater than 50. It calls the test() function on each array element till it returns true.
Since all the array elements are lesser than 50, test() returns false for all elements and hence some() also returns false.
Supplying inline callback function
In all the examples so far, the argument function supplied to some function was defined externally.
It is also possible to define the function directly as an argument to some is called. Example,

let numbers = [1, 57, 64, 99, 23];

function checkExists(valueToCheck) {
   let result = numbers.some(function(value){
      return valueToCheck === value;
   });
   document.write(result);
}

checkExists(1);   // will print true 
checkExists(112); // will print false

Above example checks if a given value is present in an array. It supplies an inline function to some() with one argument.
This argument is the current array element in each call to this function.
The function compares the array element with the value to be checked and returns true if the value exists in the array.


Supplying arrow functions as callback
Starting ES6, it is also possible to supply javascript arrow functions in place of regular functions.
With arrow functions, the example which checks if an array element is more than 3 characters can be modified as

let words = ["of", "to", "into", "in"];

let result = words.some((item) => { return item.length > 3});
document.write(result); // will print true

This syntax is much shorter and cleaner.

Hope this article helped out in understanding the syntax and usage of some() function in javascript.

Leave a Reply