Open In App

JavaScript Array some() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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

  • 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 is 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.

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.

JavaScript




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

  • In this example the Function checkAvailability checks if a value exists in an array using some().
  • Function func tests checkAvailability to see if 2 and 87 exist in the array.
  • Returns true if either 2 or 87 exists in the array [2, 5, 8, 1, 4].

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.

JavaScript




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

  • In this example the Function isGreaterThan5 checks if any element in an array is greater than 5.
  • Function func uses some() method to verify if any element in array satisfies condition.
  • Output is true if any element in array [2, 5, 8, 1, 4] is greater than 5.

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.

JavaScript




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

  • In this example the Function isGreaterThan5 checks if any element in an array is greater than 5.
  • Function func uses some() to verify if any element in array satisfies condition.
  • Output is true if any element in array [-2, 5, 3, 1, 4] is greater than 5.

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

Supported Browsers:



Last Updated : 04 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads