JavaScript some()

Michael Abe
3 min readDec 2, 2021

As we have discussed many times, arrays and navigation through them is a fundamental part of coding in general. Obviously, JavaScript is no exception to that concept and by default has many different built-in functions to assist in that endeavor. In my opinion, one of the lesser known tools to aid in the manipulation and navigation of arrays is the JS method “sort()”.

Like many other built-in methods, JS “sort()” is meant to be chained on the array that you are working with. The interesting thing that sort does to the array that you chain it to is that it essentially just checks the elements in the array to see if any of them pass the test that you send as an argument. The result of running the some method with a test that you provide is simply ‘true’ or ‘false’. Some doesn’t check beyond that simple question. Ultimately as long as one element of the array passes the test that you provide, you will get the result ‘true’ if none of them do, you will get ‘false’.

Depicted above is an example using the “some()” method. As we can see we have two arrays (array1 and array2) and two different tests. The first test “even” just takes in an element an asks if the remainder of that element being divided by two is zero. If the that is the case, the number must be divisible by two and is therefore an even number. The second test “string” takes the element that it is passed and asks if the result of “typeof” is absolute equal to “string” if it is, then the element is a string.

When we log to the console first array (“array1”) chained with the “some()” method and passing our two tests to it (lines 7 and 8 in the example above) we see that we get the results “true” for our “even” inquiry and “false” for our “string” test. This is the expected result because, as we know, the first array does contain an even number and it is made solely of numbers so will fail our “string” test. When we do the same thing and use “some()” to ask our tests on the second array we see the inverse since “array2” has substituted the even numbers from “array1” in lieu of the string representation for the. As expected, “array2” fails the “even” test and passes the “string” inquiry.

This is a simple way to use an interesting method. The fact that the “some()” method only returns “true” and “false” can make it a suitable choice if you need to scan an array for something specific. This simplicity does leave it as a less than desirable route to take if you need more in-depth and specific results as far as the nature of an array is concerned. One potential use for “some()” that I would consider is just making sure that all the elements of an array were a specific type similar to the “string” test that we used in the example above.

--

--