Suggest an editImprove this articleRefine the answer for “Array every() method”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`every()` checks that all elements of an array satisfy the condition in the callback and returns `true` or `false`.** If every element returns `true`, the method returns `true`; as soon as an element makes the callback return `false`, the check stops and the result is `false`. The source array is not modified, and for an empty array `every()` returns `true`. ```javascript const numbers = [2, 4, 6, 8]; console.log(numbers.every(num => num % 2 === 0)); // true ``` **Key point:** `every()` means "all", `some()` means "at least one", and an empty array gives `true` because of vacuous truth.Shown above the full answer for quick recall.Answer (EN)Image**`every()` checks that all elements of an array satisfy the condition defined by the callback you pass in.** If every element returns `true`, the method returns `true`; if at least one element does not match, it returns `false` and stops checking the rest. ## Theory ### TL;DR - `every()` returns `true` only when all elements satisfy the condition. - Iteration stops at the first `false` returned by the callback. - The method does not modify the source array and always returns a boolean. - For an empty array the result is `true`, a well known quirk that interviewers like to ask about. - `some()` means "at least one", `every()` means "all". ### Quick example Let us check whether an array consists of even numbers only. ```javascript const numbers = [2, 4, 6, 8]; const allEven = numbers.every(num => num % 2 === 0); console.log(allEven); // true ``` Add one odd number and the result immediately becomes false: ```javascript [2, 4, 5, 8].every(n => n % 2 === 0); // false ``` ### Signature and key points ```javascript array.every((element, index, array) => { // return true or false }); ``` Worth saying out loud in an interview: - `every()` does not modify the source array. - As soon as an element makes the callback return `false`, execution stops. - The method returns a boolean, not an element and not an array. - It works with the logical result of the callback, so any returned value is treated as truthy or falsy. - For an empty array `every()` returns `true`. ### An example from real development Checking whether all users have passed verification. ```javascript 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 ``` Iteration stops at `Kate`, because it is already clear that the "everyone is verified" condition is broken. ### Why an empty array returns true `every()` verifies the statement "all elements satisfy the condition". If there are no elements, there is no element that breaks the condition either, so the result is considered true by default. ```javascript console.log([].every(x => x > 0)); // true ``` This property is called vacuous truth. In practice it means that `every()` often needs a separate non emptiness check in front of it, otherwise "all line items are paid" is true for an order with no line items at all. ### The difference between every() and some() - `some()` requires at least one element to match the condition. - `every()` requires all elements to match the condition. - If `every()` returned `true`, then `some()` with the same condition returns `true` on a non empty array, but on an empty array `every()` gives `true` while `some()` gives `false`. - If `some()` returned `false`, it means no element matches, so `every()` with the same condition is also `false` on a non empty array. ```javascript const ages = [18, 22, 30, 16]; console.log(ages.every(age => age >= 18)); // false, not everyone is an adult console.log(ages.some(age => age < 18)); // true, there is at least one minor ``` ### Common mistakes **1. Forgetting about the empty array.** A validation such as `items.every(i => i.isValid)` will happily accept an empty list as valid. Add a `items.length > 0` check when that matters. **2. A missing `return` in a callback with curly braces.** The callback returns `undefined`, which is falsy, so the result is always `false`. ```javascript arr.every(num => { num > 5 }); // always false ``` The fix: `arr.every(num => num > 5)` or `arr.every(num => { return num > 5 })`. **3. Expecting `every()` to hand back an element or an array.** The method returns only `true` or `false`; to get an element use `find()`, to get a subset use `filter()`. **4. Confusing it with `some()`.** Swapping one method for the other raises no error, it silently inverts the logic of the check, and the bug is usually noticed in production.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.