JavaScript every Method

In the previous lesson, we learned how some() checks whether at least one item passes a test. Now letโ€™s flip that idea around and use every() to check whether all the items pass.

โœ… What is the every Method?

The every() method tests every item in an array against a condition you provide. It returns true only when all items pass, and false as soon as one item fails.

You give every() a callback function that returns a boolean for each item. If the callback returns true for every single item, the final result is true.

every.js
const numbers = [4, 8, 12, 16];
const allPositive = numbers.every((number) => number > 0);
console.log(allPositive); // true

Letโ€™s walk through what each line does:

  • const numbers = [4, 8, 12, 16] creates the array we want to test.
  • numbers.every((number) => number > 0) runs the callback number > 0 once for each item, checking that every number is greater than zero.
  • Every item passes the test, so every() returns true, and console.log prints true.

๐Ÿ›‘ It Stops Early

The every() method does not always check the whole array. The moment one item fails the test, it stops and returns false. There is no point in checking the rest, because one failure already means โ€œnot allโ€ passed.

every.js
const numbers = [4, -8, 12, 16];
const allPositive = numbers.every((number) => number > 0);
console.log(allPositive); // false

Here is what happens step by step:

  • every() checks 4, which passes the test number > 0.
  • It then checks -8, which fails because it is not greater than zero.
  • That first failure makes every() stop immediately and return false, so the items 12 and 16 are never checked.

Returns a boolean, not items

every() always returns true or false. It does not give you back a new array of items. If you want the items that pass, use filter() instead.

๐Ÿ”€ every vs some

These two methods look almost identical, but they answer opposite questions. Use some() when you care about at least one match, and every() when you need all items to match.

Method Question it answers Returns true when
some() Does at least one item pass? Any one item passes
every() Do all items pass? Every item passes

Here is the same array checked with both methods so you can see the difference:

every.js
const scores = [70, 85, 40, 90];
console.log(scores.some((score) => score >= 50)); // true โ†’ some are 50+
console.log(scores.every((score) => score >= 50)); // false โ†’ not all are 50+

Letโ€™s compare the two results on the same scores array:

  • some((score) => score >= 50) returns true because 70, 85, and 90 are all 50 or higher, so at least one item passes.
  • every((score) => score >= 50) returns false because 40 is below 50, and that single failure means not all items pass.

๐Ÿ“‹ A Practical Example

The every() method shines when you need to confirm that a whole list meets a rule. A common case is checking that all form fields are filled before letting the user submit.

every.js
const formFields = ["Alex", "alex@example.com", "Password123"];
const allFilled = formFields.every((field) => field.trim() !== "");
console.log(allFilled); // true

Letโ€™s break this down:

  • field.trim() removes any surrounding whitespace from each field so a value of only spaces counts as empty.
  • The callback field.trim() !== "" then checks that the trimmed field is not an empty string.
  • Every field has real content, so every() returns true, meaning the form is ready to submit.

It works just as well on a list of objects. Here we check whether every user has verified their account:

every.js
const users = [
{ name: "Alex", verified: true },
{ name: "Jordan", verified: true },
{ name: "Sam", verified: false },
];
const allVerified = users.every((user) => user.verified);
console.log(allVerified); // false โ†’ Sam is not verified

Here is how every() works through the objects:

  • The callback user.verified reads the verified property from each user object and uses it as the boolean result.
  • Alex and Jordan both have verified: true, so they pass.
  • Sam has verified: false, so every() stops there and returns false, which is exactly what you want before granting access to everyone.

๐Ÿซ™ Empty Arrays Return true

There is one surprising rule to remember. When you call every() on an empty array, it returns true. This is because there are no items to fail the test, so the condition is technically satisfied for all of them.

every.js
const empty = [];
console.log(empty.every((item) => item > 0)); // true

The array has no items, so the callback item > 0 never runs, and with nothing to fail the test, every() returns true.

Guard against empty lists

If an empty array should not count as โ€œall passedโ€ in your app, check the length first: arr.length > 0 && arr.every(...).

โš ๏ธ Common Mistakes to Avoid

Mistake Problem Solution
Confusing some() with every() some() checks for any match, not all Use every() when all items must pass
Expecting items back every() returns true or false, not an array Use filter() to get matching items
Forgetting empty-array behavior An empty array returns true unexpectedly Check arr.length > 0 first when needed

๐Ÿ”ง Try It Yourself!

  1. Create an array of numbers and use every() to check whether they are all positive.
  2. Change one number to be negative and run it again to see every() return false.
  3. Make an array of user objects with a verified property and check whether every user is verified.
  4. Call every() on an empty array and confirm it returns true.

๐Ÿงฉ What Youโ€™ve Learned

  • โœ… every() returns true only when all items pass the test
  • โœ… The callback returns a boolean for each item
  • โœ… every() stops early at the first failing item and returns false
  • โœ… some() checks for at least one match, while every() checks for all
  • โœ… An empty array always returns true with every()

๐Ÿš€ Whatโ€™s Next?

Now that you can check and transform arrays, letโ€™s move on to objects and how we build them. Letโ€™s continue to Creating Objects.

Share & Connect