Table of Contents
What some() Does
Returns true or false: a plain boolean, not an item and not an index.
One match is enough: it doesn't care whether one item passes or ten.
Stops at the first match: the rest of the array never gets checked.
Leaves the original array alone: it only reads through it.
let numbers = [1, 3, 5, 8, 9]; let hasEven = numbers.some(n => n % 2 === 0); console.log(hasEven); // true, because of the 8
Syntax and Parameters
array.some(function (item, index, array) {
return condition;
});
item: the current element being checked.
index: its position in the array.
array: the full array. You'll rarely need it.
The function must return true or false: that decides whether the item counts as a match.
Practical Examples
let users = [
{ name: "Vi", isAdmin: false },
{ name: "Sam", isAdmin: false },
{ name: "Alex", isAdmin: true }
];
console.log(users.some(user => user.isAdmin)); // true
Checking a form for empty fields:
let fields = ["Vi", "", "vi@email.com"]; console.log(fields.some(field => field === "")); // trueChecking if any product is out of stock:
let products = [
{ name: "Laptop", stock: 5 },
{ name: "Mouse", stock: 0 }
];
console.log(products.some(p => p.stock === 0)); // true
Things to Remember
It stops early: once it finds a match, it doesn't check anything after that.
let numbers = [2, 4, 7, 8, 10];
numbers.some(n => {
console.log(`Checking ${n}`);
return n % 2 !== 0;
});
// Checking 2
// Checking 4
// Checking 7
An empty array always gives false: there's nothing in it that could match.
console.log([].some(n => n > 0)); // falseIt only gives a yes or no: if you need the matching item, use find() instead.
Common Mistakes
Using find() just to check existence: some() says what you mean and returns a clean boolean.
Forgetting to return a value: with curly braces, you need an explicit return, otherwise it always returns false.
Using includes() for objects: includes() compares references, so use some() with a condition on the property instead.
Conclusion
Frequently Asked Questions (FAQs)
2.What's the difference between some() and includes()?Always false, since there's nothing to match.
3.Does some() change the original array?includes() checks for an exact value. some() takes a condition, so it can handle objects and more complex logic.
No. It only reads through the array.
0 Comments