Online Compiler logoOnline Compiler
Array Methods

JavaScript some() & every()

Test array elements with boolean-returning methods to check conditions efficiently.

some() Method

The some() method checks if at least one element in the array passes the test. Returns true if any element matches, false otherwise.

some() Method Example

const numbers = [2, 4, 6, 7, 8];
const hasOdd = numbers.some(n => n % 2 !== 0);
console.log(hasOdd); // true (7 is odd)

Using some() to check if at least one element matches a condition

every() Method

The every() method checks if all elements in the array pass the test. Returns true only if all elements match, false if any doesn't.

every() Method Example

const numbers = [2, 4, 6, 8];
const allEven = numbers.every(n => n % 2 === 0);
console.log(allEven); // true (all are even)

Using every() to check if all elements match a condition

Practical Examples

Check if Any User is Admin

Check if Any User is Admin

const users = [
  { name: 'Alice', role: 'user' },
  { name: 'Bob', role: 'admin' },
  { name: 'Charlie', role: 'user' }
];

const hasAdmin = users.some(user => user.role === 'admin');
console.log(hasAdmin); // true

Using some() to check if at least one user has admin role

Check if All Students Passed

Check if All Students Passed

const scores = [75, 82, 91, 88, 79];
const minScore = 70;
const allPassed = scores.every(score => score >= minScore);
console.log(allPassed); // true

Using every() to check if all scores meet the minimum requirement

Validate Email in List

Validate Email in List

const emails = ['user@example.com', 'admin@test.org', 'invalid.email'];
const isValidEmail = (email) => email.includes('@');

const hasInvalid = emails.some(email => !isValidEmail(email));
console.log(hasInvalid); // true (invalid.email has no @)

Using some() to check if any email is invalid

Check All Prices are Above Minimum

Check All Prices are Above Minimum

const prices = [19.99, 29.99, 39.99, 9.99];
const minPrice = 10;
const allAffordable = prices.every(price => price >= minPrice);
console.log(allAffordable); // false (9.99 < 10)

Using every() to check if all prices meet the minimum requirement

some() vs every() Comparison

MethodStops WhenReturns true if
some()Finds first matchAt least ONE matches
every()Finds first non-matchALL elements match

Performance Notes

💡 Tip: Both some() and every() are optimized to stop iterating once they have a definitive answer:
- some() stops as soon as it finds a matching element
- every() stops as soon as it finds a non-matching element

Common Mistakes

❌ Confusing some() and every()

Confusing some() and every()

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

// some: is there at least one even number?
console.log(arr.some(n => n % 2 === 0)); // true (2, 4)

// every: are ALL numbers even?
console.log(arr.every(n => n % 2 === 0)); // false (1, 3, 5 odd)

Understanding the difference between some() and every() methods

❌ Using some() When You Need filter()

Using some() When You Need filter()

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

// Wrong - some() only tells you if at least one matches
arr.some(n => n > 3); // true, but you lose the values

// Better - filter() gives you all matching elements
const result = arr.filter(n => n > 3); // [4, 5]

Using the wrong method for the task - some() vs filter()

FAQs

What happens with an empty array?

Empty Array Behavior

const empty = [];
console.log(empty.some(n => n > 5)); // false
console.log(empty.every(n => n > 5)); // true

How some() and every() behave with empty arrays

Are some() and every() fast?

Yes! They stop early once they know the answer. some() stops at first true, every() stops at first false.