DEV Community

Discussion on: Quick JavaScript Tip: the some() method

Collapse
 
attacomsian profile image
Atta • Edited

I think .includes() is better than .some() for checking if an array contains a value or not:

const animals = ['dogs', 'cats', 'snakes', 'birds', 'pandas'];
animals.includes('snakes'); // true

.some() is good for other use-cases like finding if a value > 15 exists in an array:

[2, 15, 18, 5, 4].some(x => x > 15);  // true
Collapse
 
mattsparks profile image
Matt Sparks

I agree, .includes() would be a better solution for the specific problem I laid out. I just wanted to give a simple bit of code to illustrate how .some() works.

I'll try and add a second example to show another use-case.

Thanks!

Collapse
 
washingtonsteven profile image
Steven Washington

I was thinking the same string, but some reminds me of a function I wrote a long time ago (in PHP) called someValidStrings which checked an array to make sure that there was at least 1 value that was correctly typed as a String, and also fulfilled some other business logic on what was considered "valid" (think: string length, ends with a run of 3 numbers, etc.). This is a case that some would excel at with the function callback rather than just looking for a certain value.

I was thinking that I could use find for the same purpose, but I see that there's a logical benefit to returning a boolean directly rather than a value that would have to be checked. I imagine find, some and findIndex work very similarly and only really differ in what they return.