Programming Useful

Use Array.some() and Array.every()

Checking if any/all items meet a condition requires manual loops with break statements. Use Array.some() to check if any item matches, Array.every() for all ite...

Problem

Checking if any/all items meet a condition requires manual loops with break statements.

Solution

Use Array.some() to check if any item matches, Array.every() for all items.

Benefit

More readable and stops execution early for better performance.

Code Example

const numbers = [1, 2, 3, 4, 5];

// Check if any number > 3
const hasLarge = numbers.some(n => n > 3); // true

// Check if all numbers > 0
const allPositive = numbers.every(n => n > 0); // true

ES
Edrees Salih
6 hours ago

We are still cooking the magic in the way!