JavaScript every Method
Table of Contents + โ
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.
const numbers = [4, 8, 12, 16];
const allPositive = numbers.every((number) => number > 0);console.log(allPositive); // trueLetโ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 callbacknumber > 0once for each item, checking that every number is greater than zero.- Every item passes the test, so
every()returnstrue, andconsole.logprintstrue.
๐ 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.
const numbers = [4, -8, 12, 16];
const allPositive = numbers.every((number) => number > 0);console.log(allPositive); // falseHere is what happens step by step:
every()checks4, which passes the testnumber > 0.- It then checks
-8, which fails because it is not greater than zero. - That first failure makes
every()stop immediately and returnfalse, so the items12and16are 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:
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)returnstruebecause70,85, and90are all 50 or higher, so at least one item passes.every((score) => score >= 50)returnsfalsebecause40is 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.
const formFields = ["Alex", "alex@example.com", "Password123"];
const allFilled = formFields.every((field) => field.trim() !== "");console.log(allFilled); // trueLetโ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()returnstrue, 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:
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 verifiedHere is how every() works through the objects:
- The callback
user.verifiedreads theverifiedproperty 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, soevery()stops there and returnsfalse, 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.
const empty = [];
console.log(empty.every((item) => item > 0)); // trueThe 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!
- Create an array of numbers and use
every()to check whether they are all positive. - Change one number to be negative and run it again to see
every()returnfalse. - Make an array of user objects with a
verifiedproperty and check whether every user is verified. - Call
every()on an empty array and confirm it returnstrue.
๐งฉ What Youโve Learned
- โ
every()returnstrueonly when all items pass the test - โ The callback returns a boolean for each item
- โ
every()stops early at the first failing item and returnsfalse - โ
some()checks for at least one match, whileevery()checks for all - โ
An empty array always returns
truewithevery()
๐ 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.