some() and every()
some() & every()
Section titled “some() & every()”Introduction
Section titled “Introduction”some() checks if at least one element passes a test. every() checks if all elements pass a test. Both return a boolean.
Visual Explanation
Section titled “Visual Explanation”flowchart TD A["some() — OR condition"] B["every() — AND condition"]
A --> A1["[1, 2, 3].some(x => x > 2)"] A1 --> A2["true — 3 is > 2"]
B --> B1["[1, 2, 3].every(x => x > 0)"] B1 --> B2["true — all are > 0"] B1 --> B3["[1, 2, 3].every(x => x > 2)"] B3 --> B4["false"]Examples
Section titled “Examples”const numbers = [1, 2, 3, 4, 5];
numbers.some(x => x > 4); // true — 5 qualifiesnumbers.every(x => x > 0); // truenumbers.every(x => x > 3); // falseUse Cases
Section titled “Use Cases”// Validationconst formFields = [ { value: 'Alice', required: true }, { value: '', required: true }, { value: 'alice@email.com', required: true }];
const allFilled = formFields.every(f => f.value !== '');const anyEmpty = formFields.some(f => f.value === '');Summary
Section titled “Summary”some()— at least one passes (short-circuits on true)every()— all pass (short-circuits on false)- Both return boolean values