JavaScript Array some() Method - GeeksforGeeks

04 Oct, 2021
Below is the example of Array some() method.
Example:<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>
<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>
Output:true
false

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

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

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

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:
<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>
Output:
true

Program 2:
<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

Supported Browsers: The browsers supported by JavaScript Array some() method are listed below:
Google Chrome 1 above
Edge 12 and above
Firefox 1.5 and above
Internet Explorer 9 and above
Opera 9.5 and above
Safari 3 and above
arorakashish0911
ysachin2314
javascript-array
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills