some() in an array
The some() method is one of the boolean array methods in JavaScript.
It checks whether at least one element satisfies the condition, and returns true or false.
As soon as the first matching element is found, the iteration stops.
Syntax
javascript
const result = array.some((element, index, array) => {
return condition;
});Callback parameters:
| Argument | Description |
|---|---|
element | The current array element |
index | The index of the current element |
array | The original array itself |
Example 1. Checking if there are even numbers
javascript
const numbers = [1, 3, 5, 6, 7];
const hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven); // true
some()returnedtruebecause the array has at least one even number (6).
Example 2. Checking if there are adult users
javascript
const users = [
{ name: 'Tim', age: 17 },
{ name: 'Alex', age: 21 },
{ name: 'John', age: 16 }
];
const hasAdult = users.some(user => user.age >= 18);
console.log(hasAdult); // true
some()stops as soon as it finds the first adult user.
Example 3. Checking for the presence of a value
javascript
const fruits = ['apple', 'banana', 'cherry'];
const hasBanana = fruits.some(fruit => fruit === 'banana');
console.log(hasBanana); // true
const hasMango = fruits.some(fruit => fruit === 'mango');
console.log(hasMango); // falseExample 4. Working with an empty array
javascript
console.log([].some(x => x > 0)); // falseFor an empty array
some()always returnsfalse, because there are no elements.
Common errors
- Expecting an array instead of a boolean
javascript
const result = arr.some(x => x > 10);
console.log(result.length); // TypeError - some() returns true/false- A forgotten
return
javascript
arr.some(num => { num > 5 }); // always falseCorrect:
javascript
arr.some(num => num > 5);
// or
arr.some(num => { return num > 5 });- Confusing it with
every()
some()-> at least one element passes the conditionevery()-> all elements pass the condition
When to use some()
| Goal | Is some() suitable |
|---|---|
| Check that at least one element matches the condition | Yes |
| Check that all elements match the condition | No - use every() |
| Find the element itself | No - use find() |
| Get a list of matching elements | No - use filter() |
Briefly:
some()returnstrueif at least one array element satisfies the condition.
Formula to remember:
arr.some(condition) -> true | false
Example for clarity
javascript
const arr = [1, 2, 3, 4, 5];
console.log(arr.some(x => x > 3)); // true
console.log(arr.some(x => x < 0)); // falseComparison with similar methods
| Method | Returns | Condition | Stops after the first match |
|---|---|---|---|
| some() | true / false | At least one element matches | Yes |
| every() | true / false | All elements match | Yes (on the first false) |
| find() | Element or undefined | At least one element matches | Yes |
| filter() | New array | All matching elements | No |
Example comparing some() and every()
javascript
const ages = [18, 22, 30, 16];
console.log(ages.some(age => age < 18)); // true -> there is at least one minor
console.log(ages.every(age => age >= 18)); // false -> not everyone is an adultShort Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.