Javascript Array some()

The JavaScript Array some() method tests whether any of the array elements pass the given test function.

The syntax of the some() method is:

arr.some(callback(currentValue), thisArg)

Here, arr is an array.


some() Parameters

The some() method takes in:

  • callback - The function to test for each array element. It takes in:
    • currentValue - The current element being passed from the array.
  • thisArg (optional) - Value to use as this when executing callback. By default, it is undefined.

Return value from some()

  • Returns true if an array element passes the given test function (callback returns a truthy value).
  • Otherwise, it returns false.

Notes:

  • some() does not change the original array.
  • some() does not execute callback for array elements without values.

Example: Check Value of Array Element

function checkMinor(age) {
    return age < 18;
}

const ageArray = [34, 23, 20, 26, 12];
let check = ageArray.some(checkMinor); // true

if (check) {
    console.log("All members must be at least 18 years of age.")
}

// using arrow function
let check1 = ageArray.some(age => age >= 18); // true
console.log(check1)

Output

All members must be at least 18 years of age.
true

Recommended Readings: JavaScript Array every()