DEV Community

loading...

Discussion on: JavaScript Array.some() or Array.every()?

Collapse
lukeshiru profile image
△ LUKE知る • Edited

One great thing about Array.prototype.some and Array.prototype.every is that they don't traverse the entire array if they found a true case for some, or a false case for every. So...

[false, false, true, false, false].some(Boolean); // stops in index 2
[true, false, true, true, true].every(Boolean); // stops in index 1
Enter fullscreen mode Exit fullscreen mode

Another good method of Array that is not used nearly enough is Array.prototype.includes, which can replace Array.prototype.indexOf in several scenarios:

const array = [0, 1, 2, 3, 4, 5];

array.includes(3); // true
// instead of
array.indexOf(3) >= 0;
Enter fullscreen mode Exit fullscreen mode

Thanks for sharing, Rakhi!

Collapse
atbrakhi profile image
Rakhi Author • Edited

Definitely!!

For the includes() and indexOf(), I agree! I have personally used both methods in my codebase, I might end up writing another post for them.

Thanks for sharing Luke.