Check whether every array element passes a test in JavaScript

items.every(test) returns true when every visited element passes and false at the first failure. An empty array returns true because there is nothing to check. Check the length separately if you require at least one entry.

Write a predicate and stop at the first failure

The callback receives the value, index, and original array. Run the snippet in the console. It stops at index 2, where the value is 3. The value at index 3 is never checked. every() itself does not modify the array.

const scores = [10, 20, 3, 40];
const visited = [];
const passed = scores.every((score, index) => {
  visited.push(index);
  return score >= 5;
});
console.log(passed);  // false
console.log(visited); // [0, 1, 2]

Use an explicit return in an arrow function with braces. An omitted return produces undefined and fails at the first visited value. To ask whether at least one item passes, use some().

Understand empty arrays and holes

For ordinary arrays, the callback skips holes. In the example, [10, , 20] passes because the empty position is skipped. [10, undefined, 20] fails because the second value is checked. See ECMAScript: every() for how empty arrays and holes are handled.

const scores = [];
console.log(scores.every(score => score >= 5)); // true
console.log(scores.length > 0 && scores.every(score => score >= 5)); // false

const sparse = [10, , 20];
console.log(sparse.every(score => score >= 5)); // true
console.log([10, undefined, 20].every(score => score >= 5)); // false

Require an input and a number at every index

For an array known to have no holes, values.length > 0 && values.every(test) is enough. To reject holes too, compare the number of calls with the length. The example uses Object.hasOwn() and Number.isFinite() to require an own finite number at each index and check the minimum.

function allAtLeast(values, minimum) {
  if (values.length === 0 || !Number.isFinite(minimum)) return false;

  let checked = 0;
  const passed = values.every((value, index) => {
    checked += 1;
    return Object.hasOwn(values, index) &&
      Number.isFinite(value) && value >= minimum;
  });
  return passed && checked === values.length;
}
console.log(allAtLeast([7, 8], 5)); // true
console.log(allAtLeast([7, 3], 5)); // false
console.log(allAtLeast([], 5));     // false
console.log(allAtLeast([7, , 8], 5)); // false
console.log(allAtLeast(['7', 8], 5)); // false

When converting a list of strings to numbers, check empty entries separately from numeric conversion. Number('') returns 0, so the converted result cannot identify a blank entry.

Complete example: show which indexes were checked

Save the HTML in UTF-8 as array-every.html and open it in a browser. The defaults give false and indexes 0, 1, 2. Change the minimum from 5 to 2 and click Check all to get true and indexes 0, 1, 2, 3.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Check every array value</title>
</head>
<body>
<label for="numbers">Numbers separated by commas (blank means an empty array)</label>
<input id="numbers" type="text" value="10, 20, 3, 40">
<label for="minimum">Minimum</label>
<input id="minimum" type="number" step="any" value="5">
<button id="check" type="button">Check all</button>
<pre id="output" aria-live="polite"></pre>
<script>
const numbers = document.querySelector('#numbers');
const minimum = document.querySelector('#minimum');
const output = document.querySelector('#output');

function check() {
  const raw = numbers.value.trim();
  const parts = raw === '' ? [] : raw.split(',').map(part => part.trim());
  const values = parts.map(part => Number(part));
  const limit = minimum.valueAsNumber;
  if (parts.some(part => part === '') ||
      values.some(value => !Number.isFinite(value)) ||
      !Number.isFinite(limit)) {
    output.textContent = "Enter comma-separated numbers and check the minimum.";
    return;
  }
  const visited = [];
  const allPassed = values.every((value, index) => {
    visited.push(index);
    return value >= limit;
  });
  output.textContent = `Array: ${JSON.stringify(values)}\n` +
    `every() result: ${allPassed}\n` +
    `Indexes checked: ${visited.join(', ') || "(none)"}\n` +
    `At least one value and all pass: ${values.length > 0 && allPassed}`;
}

document.querySelector('#check').addEventListener('click', check);
check();
</script>
</body>
</html>

Clear the whole list and every() gives true, while the condition requiring input is false. An entry like 10,,20 or a missing minimum shows a message. Use commas between values and a period as the decimal separator in this example.