Tutorialspoint.dev

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:

<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>

Output:

true

Program 2:

// 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>

Output:

false

Program 3:

<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>

Output:

true
false


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter