Javatpoint Logo
Javatpoint Logo

JavaScript Array some() Method

The 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.

Syntax

Parameter

callback_funct: It is the function that tests each element present in the array. It undertakes the following three arguments:

  • element: It is the current element, being in process.
  • index: Although it is optional, it is the index value of the current element in process.
  • arr: It is the given array on which some() method performs the test.

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.

Return

It 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:

  • Before the first calling of function, it is required to set the range of the elements to implement some() method.
  • The callback function will not visit those elements added after invoking some() method.
  • It does not work on the deleted array elements.
  • If the callback function changes an existing and unchanged element, the value gets defined to the array.

JavaScript Array some() Method Example

Let's implement some examples to understand better:

Example1

Here's a simple implementation of the some() method.

Test it Now

Output:

It tests whether any element is greater than 80. Thus, one element is present that is satisfying the condition. So, some() function returns True.

JavaScript Array some() Method

Example2

Testing if any array element contains the same match.

Test it Now

Output:

JavaScript Array some() Method

Example3

Checking whether any array element is found even.

Test it Now

Output:

JavaScript Array some() Method

Example4

We can also implement the above example using Arrow function.

Test it Now

Output:

JavaScript Array some() Method

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA