JS: Array.prototype.some

By Xah Lee. Date: . Last updated: .
xArray.some(f)
  • Return true if the function f return true for at least one element.
  • As soon as f return true, exit the iteration and return true.

f is given 3 args: currentElement currentIndex xArray

[1, 2, 3, 4, 5, 6].some(((x) => (x === 3))) === true
xArray.some(f, thisArg)

Use thisArg for this (binding) of f

Example. As Loop with Break

some is useful functional programing style of a loop that exit when a condition is met. (similar to forEach with break.)

Here is example. Apply f to each element in order. Exit the loop when f returns true.

const ff = ((x) => {
  console.log(x);
  return x === 3;
});

[1, 2, 3, 4, 5, 6].some(ff);

/*
prints
1
2
3
*/

Example. on Empty Array

some on empty array returns false.

[].some((x) => true) === false

JavaScript. Array Reduce, Fold

JavaScript. Map, Iteration, Reduce