Skip to main content

every() in an array

The every() method checks that all elements of the array satisfy a condition set by the passed callback function. If every element returns true, the method returns true. If at least one element does not match, it returns false and stops further checking.

Important points worth mentioning in an interview

  • every() does not change the original array.
  • As soon as an element is found for which the callback returned false, execution stops.
  • Returns a boolean.
  • Works only with the callback's logical result.
  • For an empty array every() returns true (this is an important, frequently asked feature).

Signature

js
array.every((element, index, array) => { // return true/false });

Example

Let's check whether an array consists only of even numbers:

js
const numbers = [2, 4, 6, 8]; const allEven = numbers.every(num => num % 2 === 0); console.log(allEven); // true

If we add an odd number:

js
[2, 4, 5, 8].every(n => n % 2 === 0); // false

Example from real-world development

Checking whether all users have passed verification:

js
const users = [ { name: 'Alex', verified: true }, { name: 'John', verified: true }, { name: 'Kate', verified: false }, ]; const allVerified = users.every(u => u.verified); console.log(allVerified); // false

Common pitfalls and interview questions

1) Why does an empty array give true?

Because every() checks the condition "all elements satisfy". If there are no elements, there is no violation of the condition, so the result is considered true by default. This property is often called "vacuous truth".

2) Difference from some():

  • some() -> at least one element must match
  • every() -> all elements must match
  • In short:
    • if every() returned true, some() can be either true or false
    • but if some() returned false, that does not mean every() will be true

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.