Syntax
arr.some(item => condition)
arr.every(item => condition)Examples
some() - At Least One Match
Checking whether any element satisfies a condition.
const numbers = [1, 3, 5, 8, 9];
console.log(numbers.some(n => n % 2 === 0)); // true - 8 is even
console.log(numbers.some(n => n > 100)); // false - none areevery() - All Must Match
Checking whether every element satisfies a condition.
const ages = [22, 25, 30, 18];
console.log(ages.every(age => age >= 18)); // true - all are adults
console.log(ages.every(age => age >= 21)); // false - 18 fails the checkPractical Validation Example
A common real-world use: form validation.
const formFields = [
{ name: "email", value: "user@test.com" },
{ name: "password", value: "" },
{ name: "username", value: "bob" }
];
const allFilled = formFields.every(field => field.value.length > 0);
console.log(allFilled); // false - password is empty
const hasEmptyField = formFields.some(field => field.value.length === 0);
console.log(hasEmptyField); // trueBest practices
- Use some() instead of filter().length > 0 when you only need a true/false answer - it stops early, which is more efficient
- Use every() for validation checks where all items must satisfy a rule, like confirming every required field is filled
- Remember every() on an empty array always returns true (vacuous truth), and some() on an empty array always returns false
- Combine some()/every() with logical operators for readable, declarative validation logic
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().