JavaScript Array some() MethodThe some() methods perform testing and checks if atleast a single array element passes the test, implemented by the provided function. If the test is passed, it returns true. Else, returns false. Note: If some() method is applied on an empty array, it returns false always.SyntaxParametercallback_funct: It is the function that tests each element present in the array. It undertakes the following three arguments:
thisArg: It is an optional parameter, used as 'this' value while executing the callback function. If we do not provide, 'undefined' will be used as 'this' value. ReturnIt returns a boolean value. If it founds an element returning true value to the callback function, it returns true. Otherwise, false. Note: The array.some() does not affect the original array.Points to note:
JavaScript Array some() Method ExampleLet's implement some examples to understand better: Example1 Here's a simple implementation of the some() method. Test it NowOutput: It tests whether any element is greater than 80. Thus, one element is present that is satisfying the condition. So, some() function returns True. Example2 Testing if any array element contains the same match. Test it NowOutput: Example3 Checking whether any array element is found even. Test it NowOutput: Example4 We can also implement the above example using Arrow function. Test it NowOutput: Note: If some() method finds the element matching the implemented condition of the callback function at first attempt, then it stops its search and returns Boolean true. It does not traverse for each element in that case.
Next TopicJavaScript Array
|