Testing Arrays

The some and every methods test array elements against a condition and return a boolean. They are useful for validation and conditional logic.

some: Does Any Element Match?

The some method returns true if at least one element passes the test:

const numbers = [1, 2, 3, 4, 5];

const hasEven = numbers.some((n) => n % 2 === 0);
console.log(hasEven); // true

const hasNegative = numbers.some((n) => n < 0);
console.log(hasNegative); // false

some stops iterating as soon as it finds a match (short-circuit evaluation):

const numbers = [1, 2, 3, 4, 5];

numbers.some((n) => {
  console.log("Checking:", n);
  return n === 2;
});
// Logs: Checking: 1, Checking: 2
// Stops after finding 2

every: Do All Elements Match?

The every method returns true only if all elements pass the test:

const numbers = [2, 4, 6, 8, 10];

const allEven = numbers.every((n) => n % 2 === 0);
console.log(allEven); // true

const allPositive = numbers.every((n) => n > 0);
console.log(allPositive); // true

const allGreaterThan5 = numbers.every((n) => n > 5);
console.log(allGreaterThan5); // false

every stops as soon as it finds a failing element:

const numbers = [2, 4, 5, 8, 10];

numbers.every((n) => {
  console.log("Checking:", n);
  return n % 2 === 0;
});
// Logs: Checking: 2, Checking: 4, Checking: 5
// Stops after finding 5 (odd number)

Validation Patterns

These methods work well for validating data:

const users = [
  { name: "Alice", email: "alice@example.com", age: 28 },
  { name: "Bob", email: "bob@example.com", age: 34 },
  { name: "Charlie", email: "", age: 22 },
];

// Check if all users have valid emails
const allHaveEmail = users.every((user) => user.email.length > 0);
console.log(allHaveEmail); // false

// Check if any user is under 25
const hasYoungUser = users.some((user) => user.age < 25);
console.log(hasYoungUser); // true

Checking Array Contents

const permissions = ["read", "write", "delete"];

// Check if user has required permission
const canWrite = permissions.some((p) => p === "write");
console.log(canWrite); // true

// Check if all required permissions are present
const required = ["read", "write"];
const hasAll = required.every((r) => permissions.includes(r));
console.log(hasAll); // true

Empty Arrays

Pay attention to how these methods handle an empty array:

const empty = [];

console.log(empty.some((x) => x > 0)); // false (no element matches)
console.log(empty.every((x) => x > 0)); // true (no element fails!)

The result from every looks wrong at first, but it follows the logic of the statement being tested. “All elements satisfy the condition” is vacuously true when there are no elements.

Combining with Other Methods

const products = [
  { name: "Laptop", price: 999, inStock: true },
  { name: "Phone", price: 699, inStock: true },
  { name: "Tablet", price: 449, inStock: false },
];

// Are all in-stock products affordable (under $1000)?
const affordableStock = products
  .filter((p) => p.inStock)
  .every((p) => p.price < 1000);

console.log(affordableStock); // true

// Does any out-of-stock product exist?
const hasOutOfStock = products.some((p) => !p.inStock);
console.log(hasOutOfStock); // true

some vs includes

When you are just checking for a value, includes is simpler:

const roles = ["admin", "editor", "viewer"];

// Use includes for simple values
const isAdmin = roles.includes("admin");

// Use some for complex conditions
const hasAdminLike = roles.some((role) => role.startsWith("admin"));