JavaScript | Array some() function

Arr.some()
arr.some() function check whether at least one of the elements of the array satisfies the condition checked by the argument function. The syntax of the function is as follows:

arr.some(arg_function(element, index, array), thisArg)

Arguments
The argument of this function is another function that defines the condition to be checked for each element of the array. This function itself takes three arguments:

  • array
  • This is the array on which the .some() function was called.

  • index
  • This is the index of the current element being processed by the function.

  • element
  • This is the current element being processed by the function.

Another argument this Value is used to tell the function to use this value when executing argument function.

Return value
This function returns true even if one of the elements of the array satisfies the condition(and does not check the remaining values) implemented by the argument function. If no element of the array satisfies the condition then it returns false.

Examples for the above function are provided below:

Example 1:

function isGreaterThan5(element, index, array) 
{
  return element > 5;
}

print([2, 5, 8, 1, 4].some(isGreaterThan5));

Output:

true

In this example the function some() checks for any number that is greater than 5. Since there exists element that satisfy this condition, thus the function returns true.

Example 2:

function isGreaterThan5(element, index, array) 
{
  return element > 5;
}

print([-2, 5, 3, 1, 4].some(isGreaterThan5)); 

Output:

false

In this example the function some() checks for any number that is greater than 5. Since there exists no elements that satisfy this condition, thus the function returns false.

Example 3:

var arr = [2, 5, 8, 1, 4]

function checkAvailability(arr, val) 
{
  return arr.some(
           function(arrVal) 
           {
             return val === arrVal;
           } );
}

print(checkAvailability(arr, 2));
print(checkAvailability(arr, 87));

Output:

true
false

In this example the function some() checks for 2 and 87 in the array. Since only 2 is available therefore the function returns true for first query while it returns false for second query.

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() function
  
function isGreaterThan5(element, index, array) 
{
   return element > 5;
}
function func() 
{
    // Original array
    var array = [2, 5, 8, 1, 4];
  
    // Checking for condition in array
    var value = array.some(isGreaterThan5);
  
    document.write(value);
}
func();
</script>

chevron_right


Output:

true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate lastIndexOf() function
<script>
function isGreaterThan5(element, index, array) 
{
  return element > 5;
}
  
function func() 
{
    // Original array
    var array = [-2, 5, 3, 1, 4];
  
    // Checking for condition in the array
    var value = array.some(isGreaterThan5);
  
    document.write(value); 
}
  
func();
</script>

chevron_right


Output:

false

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate some() function
  
function checkAvailability(arr, val) 
{
  return arr.some(function(arrVal) 
  {
    return val === arrVal;
  });
}
function func() 
{
    // Original function
    var arr = [2, 5, 8, 1, 4]
  
    // Checking for condition
    document.write(checkAvailability(arr, 2));
    document.write("<br>");
    document.write(checkAvailability(arr, 87));
}
func();
</script>

chevron_right


Output:

true
false


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.