Friday, November 15, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Array some() Method

JavaScript Array some() Method

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

Syntax:

arr.some(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 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.

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 is an example of the Array some() method.

Example: In this example, 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

Example 1: In this example the 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

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.

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

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 the first query while it returns false for the second query.

Javascript




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


Output:

true
false

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

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

RELATED ARTICLES

Most Popular

Recent Comments