JavaScript Array some() Method

Below is the example of Array some() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

The arr.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method.

Syntax:

arr.every(callback(element[, index[, array]])[, thisArg])

Parameters: This method accepts five parameters as mentioned above and described below:

  • callback: This parameter holds the function to be called for each element of the array.
  • element: The parameter holds the value of the elements being processed currently.
  • index: This parameter is optional, it holds the index of the currentValue element in the array starting from 0.
  • array: This parameter is optional, it holds the complete array on which Array.every is called.
  • thisArg: This parameter is optional, it holds the context to be passed as this to be used while executing the callback function. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.

Return value: This method 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 method. If no element of the array satisfies the condition then it returns false.



Below Examples illustrate the method in JavaScript:

  • Example 1: In this example the method some() checks for any number that is greater than 5. Since there exists element that satisfy this condition, thus the method returns true.
    function isGreaterThan5(element, index, array) 
    {
      return element > 5;
    }
    
    print([2, 5, 8, 1, 4].some(isGreaterThan5));
    

    Output:

    true
    
  • Example 2: In this example the method some() checks for any number that is greater than 5. Since there exists no elements that satisfy this condition, thus the method returns false.
    function isGreaterThan5(element, index, array) 
    {
      return element > 5;
    }
    
    print([-2, 5, 3, 1, 4].some(isGreaterThan5)); 
    

    Output:

    false
    
  • Example 3: In this example the method some() checks for 2 and 87 in the array. Since only 2 is available therefore the method returns true for first query while it returns false for second query.
    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
    

Codes for the above method are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

<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

Supported Browsers: The browsers supported by JavaScript Array some() method are listed below:

  • Google Chrome
  • Internet Explorer
  • Mozila Firefox
  • Safari
  • Opera

full-stack-img




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.