Skip to main content

JavaScript Arrays

JavaScript Array some and every

Written by Published

JavaScript some and every check whether elements pass a test and return true or false.

some asks whether at least one element passes.

every asks whether all of them do.

Example

Example

javascript

const numbers = [1, 8, 3];

console.log(numbers.some(n => n > 5));
console.log(numbers.every(n => n > 5));

The output is true then false.

some and every

Both return a boolean rather than an array.

some stops as soon as one element passes.

every stops as soon as one element fails.

Syntax

Syntax

javascript

array.some(callback);
array.every(callback);

The callback returns true or false for each element.

Validating a Form

every is a natural fit for checking that all fields are filled.

Example

Example

javascript

const fields = ["Ada", "London", "engineer"];

console.log(fields.every(f => f.length > 0));

One empty field would make this false.

Checking for Any Problem

some answers the opposite question.

Example

Example

javascript

const scores = [70, 45, 90];

console.log(scores.some(s => s < 50));

The output is true, because one score is below 50.

Empty Arrays

This surprises people: every on an empty array is always true.

some on an empty array is always false.

Example

Example

javascript

const empty = [];

console.log(empty.every(n => n > 100));
console.log(empty.some(n => n > 0));

There is nothing to fail the test, so every passes.

Working with Objects

Both work on arrays of objects in the same way.

Example

Example

javascript

const users = [
  { name: "Ada", active: true },
  { name: "Alan", active: false }
];

console.log(users.some(u => u.active));
console.log(users.every(u => u.active));

One active user makes some true but every false.

They Stop Early

Neither method walks the whole array unless it has to.

Example

Example

javascript

const numbers = [10, 1, 2, 3];

console.log(numbers.some(n => n > 5));

The first element already answers the question, so the rest are skipped.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript some and every</title>
</head>
<body>

  <h1>JavaScript some and every</h1>

  <p id="result"></p>

  <script>
    const scores = [70, 45, 90, 60];

    document.getElementById("result").innerHTML =
      "Anyone failed: " + scores.some(s => s < 50) +
      "<br>Everyone passed: " + scores.every(s => s >= 50);
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try fixing the low score:

Change 45 to 55 and watch both answers flip.

Important Points

  • some is true when at least one element passes.
  • every is true when all elements pass.
  • Both return a boolean, not an array.
  • every on an empty array is true.
  • some on an empty array is false.

Conclusion

some and every answer yes-or-no questions about a list.

They are clearer than a loop with a flag variable.

Remember how they behave on empty arrays, which is a common interview question.