every() day some() time
Moving from Python to JavaScript, I often find myself thinking what’s the equivalent of X in JS?
Two nifty-little-tricks I use regularly are Python’s built-in functions: any()
and all()
.
Both accept an iterable and return True
if any/all element/s of the iterable are true, ‘False’ otherwise.
Today I learned their JS equivalent for arrays: some()
and every()
.
The function checks whether at least one/every element in the array passes the condition implemented by the provided callback.
Usage
[10, 11, 12, 13].every((x) => x > 10); // false
[8, 9, 10, 11].some((x) => x > 10); // true
- The 2nd callback parameter (optional) is the element index
- The 3rd (optional as well) is the array called