Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when use JS .includes() vs .some()? [closed]

some : if it finds an array element where the function returns a true value, some() returns true (and does not check the remaining values). includes : for all element is return .

Don't know right or not ?. When use some or includes ?. Thankyou everyone.

like image 817
Nguyễn Hồ Minh Nhựt Avatar asked Nov 21 '20 18:11

Nguyễn Hồ Minh Nhựt


People also ask

What is some () in JS?

some() The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false.

What can be used instead of includes in JavaScript?

indexOf() The Array#indexOf() function is a common alternative to includes() . The indexOf() function returns the first index in the array at which it found valueToFind , or -1 otherwise.

What is difference between some and find?

The difference is in the output. find will return the value, some will return a boolean .


1 Answers

some takes in a callback function where you can write your own logic to determine if an array contains some element which matches the conditions you wrote.

includes does a generic equalTo comparison on every element and will return true if at least one element in the array is equal to the value to find.

like image 58
RecursiveThinking Avatar answered Nov 15 '22 14:11

RecursiveThinking