some()

ES5+

Tests whether at least one element in the array passes the test implemented by the provided function.

Syntax

array.some(callback(element, index, array), thisArg)

Parameters

callback Function

Function to test each element

Return Value

boolean

true if at least one element passes the test

Examples

JavaScript
const numbers = [1, 2, 3, 4, 5];
const hasEven = numbers.some(x => x % 2 === 0);
console.log(hasEven);
Output:
// true

📌 When to Use

Use some() when you need to check if at least one element meets a condition. Perfect for validation checks, permission testing, or any scenario where a single match is sufficient to determine the result.

⚠️ Common Mistakes

Using some() when you need the actual matching element - use find() instead

Confusing some() with every() - some() returns true if ANY element matches

Using filter().length > 0 instead of some() for existence checks

✅ Best Practices

Use some() for boolean existence checks - it's more readable and efficient than filter().length

Combine with negation for checking if none match: !arr.some(x => x.invalid)

Use descriptive callback names for complex conditions: some(isExpired) vs some(x => ...)

⚡ Performance Notes

some() short-circuits and returns immediately when it finds a truthy result, making it very efficient for large arrays. It's faster than filter().length > 0 because it doesn't need to process all elements or create a new array.

🌍 Real World Example

Form Validation - Checking for Errors

Check if any form field has a validation error before submission

const formFields = [
  {name: 'email', value: 'test@example.com', error: null},
  {name: 'password', value: '123', error: 'Too short'},
  {name: 'username', value: 'john', error: null}
];

const hasErrors = formFields.some(field => field.error !== null);
// hasErrors: true

if (hasErrors) {
  console.log('Please fix validation errors before submitting');
} else {
  submitForm();
}

Related Methods