Open In App

JavaScript Array some() Method

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

Array some() Method Syntax

arr.some(callback(element,index,array),thisArg);

Array some() Method Parameters

Array some() Method Return value

Returns true if any of the array elements pass the test, otherwise false.



Array some() Method Example

Here, we will check whether the values are equal using the some() method.




function checkAvailability(arr, val) {
    return arr.some(function (arrVal) {
        return val === arrVal;
    });
}
function func() {
    // Original function
    let arr = [2, 5, 8, 1, 4];
 
    // Checking for condition
    console.log(checkAvailability(arr, 2));
    console.log(checkAvailability(arr, 87));
}
func();

Output

true
false


Explanation

Array some() Method Example

Here, method some() checks for any number that is greater than 5. Since there exists an element that satisfies this condition, thus the method returns true.




function isGreaterThan5(element, index, array) {
    return element > 5;
}
function func() {
    // Original array
    let array = [2, 5, 8, 1, 4];
 
    // Checking for condition in array
    let value = array.some(isGreaterThan5);
 
    console.log(value);
}
func();

Output
true


Explanation

Array some() Method Example

Here, 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;
}
function func() {
    // Original array
    let array = [-2, 5, 3, 1, 4];
 
    // Checking for condition in the array
    let value = array.some(isGreaterThan5);
 
    console.log(value);
}
func();

Output
false


Explanation

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:


Article Tags :